SEARCH

How-To Geek

Geek School: Writing Your First Full PowerShell Script

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:

 

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.

image

So now that we know where we can find the information we looking for, open up the ISE and type the following.

Get-WmiObject -Class Win32_OperatingSystem –ComputerName localhost |

Select-Object -Property CSName,LastBootUpTime

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.

image

Now click on the green “Run Script” button or press the F5 key on your keyboard to test the code.

image

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:

Select-Object -Property CSName,@{n=”Last Booted”;

e={[Management.ManagementDateTimeConverter]::ToDateTime($_.LastBootUpTime)}}

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.

image

Running the code now will yield a much more readable last boot time.

image

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:

C:\Get-LastBootTime.ps1

image

Now switch to the bottom half of the ISE and run the following:

C:\Get-LastBootTime.ps1

image

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.

param(
[string]$ComputerName
)

Then replace the hardcoded localhost value with $ComputerName variable. Your script should now look like this:

image

Save your script, then head back to the bottom half of the ISE and view the help for your script.

help C:\Get-LastBootTime.ps1

image

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.

[Parameter(Mandatory=$true)][string]$ComputerName

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.

<#
.SYNOPSIS
Shows when last your PC started up.
.DESCRIPTION
This is a WMI wrapper function to get the time that your PC last started up.
.PARAMETER ComputerName
The name of the Computer you want to run the command against.
.EXAMPLE
Get-LastBootTime -ComputerName localhost
.LINK
www.howtogeek.com
#>

Once all that is done, you should end up with a script looking like this.

image

Let’s now go and check out our new help file.

image

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.

image

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.

Taylor Gibb is a Microsoft MVP and all round geek, he loves everything from Windows 8 to Windows Server 2012 and even C# and PowerShell. You can also follow him on Google+

  • Published 04/11/13

Enter Your Email Here to Get Access for Free:

Go check your email!