ScioSoft's Community Blogs Optimized IT musings for the technically inclined

PowerShell: Get OS Installation Date

by James Fielding 18. October 2011 19:43
Here is a quick, and oh-so-easy, one-liner to see when you installed a machine’s Operating System:
([WMI]'').ConvertToDateTime((Get-WmiObject Win32_OperatingSystem).InstallDate)
The main part of the command gets the install date:
(Get-WmiObject Win32_OperatingSystem).InstallDate
The problem is, if you just run this command, the result is not so human-friendly:
20090328134854.000000-240
To fix this up, we can apply the ConvertToDate method, which is actually a Common helper method (that has nothing to do with the Win32_Service WMI class). To do this I used /\/\o\/\/’s method of creating an "Empty" WMI class for this ([WMI]''):
([WMI]'').ConvertToDateTime()
Once we do this, we get a nice, human-friendly result:
March-28-09 1:48:54 PM
Happy OS Dating (…judging by the date on my system, it looks like it is time for a reimage),
James Fielding

Sciosoft Systems is a Canadian web design & development company based in Muskoka, which is in central Ontario. We provide ASP.NET website & Windows Server application development services to small and medium-sized business, as well as local government and not-for-profit groups. If you have a website project you’d like to discuss, please visit us at www.sciosoft.com.

Share |

Tags: , ,

IT Systems | PowerShell

Launch PowerShell Script from Shortcut

by James Fielding 4. October 2011 21:42

PowerShell is a great system management tool, and can help administrators to quickly perform tasks and collect information.

One frustration many users have with PowerShell is, unlike traditional .bat files, .ps1 files will not run by default due to PowerShell's enhanced security. Now this is OK when you’re dealing with a couple of machines, as a quick PowerShell one-liner at the prompt will fix things up for you:

 Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process

But, when you're dealing with a large number of machines, and/or you'd prefer to keep PowerShell’s security settings in place, here's a quick workaround. Run the script via a shortcut that temporarily allows the script to run:

  • In Windows Explorer, create a new shortcut. It doesn’t matter what your shortcut’s target is, as we’ll change that momentarily.
  • Right-click on your new shortcut, and choose “Properties”.
  • Change the shortcut’s Target to the following:
    %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File "E:\Scripts\MyScript.ps1" 
    
    Where “E:\Scripts\MyScript.ps1” is the path to the script you want to run.
  • Click “OK”.

From here, you just need to double-click your shortcut to run your script.

Sometimes you won’t want the command window to provide output. For example, say your scripts outputs to a text file. If this is the case, simply add:

-WindowStyle Hidden

Also, if you are using this on machines that could potentially have an altered Powershell profile, you'll likely want to add:

-NoProfile

I often use this trick to run .ps1 scripts locally via a USB flashdrive. For each set of scripts that I need to run, I have a shortcut. Then, I just plug in the flashdrive, and double-click the shortcut. This way, you can quickly run scripts locally on multiple machines. Try it out!

Happy PowerShell Shortcutting,
James Fielding

Sciosoft Systems is a Canadian web design & development company based in Muskoka, which is in central Ontario. We provide ASP.NET website & Windows Server application development services to small and medium-sized business, as well as local government and not-for-profit groups. If you have a website project you’d like to discuss, please visit us at www.sciosoft.com.

Share |

Tags:

PowerShell

RecentComments

Comment RSS

The opinions expressed herein are the personal opinions of the contributors and do not necessarily represent the views of Sciosoft Systems Inc.