A few weeks ago, The Geek showed you how you can use the command prompt to find when your computer was started up last. In this last installation of Geek School for PowerShell, we are going to write a reusable PowerShell command to do the same thing.
Be sure to read the previous articles in the series:
Learn How to Automate Windows with PowerShell Learning to Use Cmdlets in PowerShell Learning How to Use Objects in PowerShell Learning Formatting, Filtering and Comparing in PowerShell Learn to Use Remoting in PowerShell Using PowerShell to Get Computer Information Working with Collections in PowerShell Learn How to Use Jobs in PowerShell Learn How to Extend PowerShell Learning PowerShell Variables, Input and Output
Writing Your First Script
The first thing we need to do is find a way to access the information we looking for. Since we are dealing with management information, we probably need to take a look at using WMI, which does indeed have a class called Win32_OperatingSystem which allows you to view verbose information about your operating system, including the last time it started up.
So now that we know where we can find the information we looking for, open up the ISE and type the following.
Note: I had to split my code over two lines so that it all fit into the screenshot, but feel free to type it on a single line. If you do choose to split it over two lines, please make sure that the pipe character is the last character on line 1.
Select-Object -Property CSName,LastBootUpTime
Now click on the green “Run Script” button or press the F5 key on your keyboard to test the code.
WMI times can be a little cryptic. By that we mean if you look at the LastBootUpTime property, it says 2013-03-19 at 18:26:21, but for some reason the WMI guys decided to concatenate all of that into a single string. Luckily for us, we don’t have to worry about manually parsing the string as there is an easier way to do it, albeit more advanced. You will need to change the Select-Object part of the code to look like this:
What we are doing here is creating a custom property called “Last Booted” and specifying that its value must be the result of calling the ToDateTime static method on the LastBootUpTime property of the current pipeline object. Your code should now look like this.
e={[Management.ManagementDateTimeConverter]::ToDateTime($_.LastBootUpTime)}}
Running the code now will yield a much more readable last boot time.
Now that we are happy with the basic functionality of our script, we need to save it. For simplicity, let’s save it as the following:
Now switch to the bottom half of the ISE and run the following:
Great! Our script is working as expected, however there is still one problem with our script. We hardcoded the name of the computer we want to get the last boot time for. Instead of hardcoding values, we should rather provide a parameter so that whoever is using the script can choose which computer they run the script against. To do that, go to the top of your script and do the following.
Then replace the hardcoded localhost value with $ComputerName variable. Your script should now look like this:
Save your script, then head back to the bottom half of the ISE and view the help for your script.
Awesome, so now we can specify the name of the computer we want to get the last boot time for using our new ComputerName parameter. Unfortunately, there are still a few things wrong. Firstly, the ComputerName parameter is optional and secondly, that’s the ugliest helpful I’ve ever seen, so let’s fix those issues quickly. To make the ComputerName parameter mandatory, change the contents of the param block to the following.
As for making a better help file, the most common method is to use comment based help. That means we just add an extra long comment to the top of out script.
Once all that is done, you should end up with a script looking like this.
Let’s now go and check out our new help file.
Ahhh, looking great! Now that our script is complete, we have one last thing to do: testing. For this, I’m going to exit out of the ISE and head back into the PowerShell console just so that we can make sure that there are no anomalies.
If you start with a simple one liner and just keep building onto it like we did in this guide, you will get the hang of it in no time. That’s all for this time folks, see you in the next installation of Geek School.