Geek School: Your First PowerShell Script - A Beginner's Guide

Automating System Startup Time Retrieval with PowerShell
Previously, The Geek demonstrated a method for determining your computer’s last startup time using the command prompt. This article, the final installment of Geek School for PowerShell, will focus on creating a reusable PowerShell command to achieve the same result.
Prerequisites: Reviewing the PowerShell Series
To fully benefit from this lesson, it’s recommended you familiarize yourself with the preceding articles in this series. These provide foundational knowledge for understanding the concepts presented here.
- PowerShell Automation: Learn how to automate tasks within Windows using PowerShell.
- Cmdlet Utilization: Gain proficiency in using cmdlets, the fundamental building blocks of PowerShell.
- Object Handling: Understand how to work with objects within the PowerShell environment.
- Formatting and Filtering: Discover techniques for formatting, filtering, and comparing data in PowerShell.
- PowerShell Remoting: Explore the capabilities of PowerShell remoting for managing remote systems.
- System Information Retrieval: Learn how to utilize PowerShell to gather detailed information about your computer.
- Working with Collections: Master the art of manipulating collections of data within PowerShell.
- PowerShell Jobs: Understand how to leverage PowerShell jobs for asynchronous task execution.
- PowerShell Extension: Discover methods for extending PowerShell’s functionality.
- Variables, Input, and Output: Gain a solid understanding of PowerShell variables, input mechanisms, and output handling.
By reviewing these earlier lessons, you’ll be well-equipped to grasp the principles behind building a reusable PowerShell command for retrieving system startup times.
This reusable command will streamline the process, allowing for quick and consistent access to this valuable system information. The focus is on creating a solution that can be easily integrated into larger scripts or used independently.
Initiating Your First Script
To begin, it's necessary to establish a method for accessing the desired information. Given that we are working with system management data, utilizing WMI is a logical approach. WMI includes a class, Win32_OperatingSystem, which provides detailed information regarding your operating system, including its last startup time.

Having identified the source of the required information, open the Integrated Scripting Environment (ISE) and enter the following commands.
Get-WmiObject -Class Win32_OperatingSystem –ComputerName localhost |
Select-Object -Property CSName,LastBootUpTime
It should be noted that the code was divided across two lines for screenshot clarity. Feel free to input it on a single line. Should you choose to split the command, ensure the pipe symbol resides at the end of the first line.

To execute the code, click the green “Run Script” button or press the F5 key on your keyboard.

The time format provided by WMI can be somewhat unconventional. Specifically, the LastBootUpTime property presents the date and time as a single concatenated string. Fortunately, manual string parsing isn't required, as a more sophisticated method exists.
You will need to modify the Select-Object portion of the code to read as follows:
Select-Object -Property CSName,@{n="Last Booted";
e={[Management.ManagementDateTimeConverter]::ToDateTime($_.LastBootUpTime)}}
This action creates a custom property named “Last Booted,” assigning it the result of applying the ToDateTime static method to the LastBootUpTime property of the current pipeline object. The complete code should now appear as shown.

Executing the code again will now display the last boot time in a more easily readable format.

With the core functionality verified, saving the script is the next step. Let's save it as:
C:\Get-LastBootTime.ps1

Switch to the lower section of the ISE and execute the following command:
C:\Get-LastBootTime.ps1

The script is functioning correctly; however, a limitation remains. The computer name is hardcoded. Instead of a fixed value, a parameter should be implemented, allowing users to specify the target computer. To achieve this, add the following to the beginning of your script:
param(
[string]$ComputerName
)
Subsequently, replace the hardcoded "localhost" value with the $ComputerName variable. The updated script should now appear as follows.

Save the script and then, in the lower section of the ISE, view the script's help information.
help C:\Get-LastBootTime.ps1

This allows specification of the computer name using the new ComputerName parameter. However, further improvements are needed. The ComputerName parameter is currently optional, and the help output is not ideal. Let's address these issues.
To make the ComputerName parameter mandatory, modify the param block to:
[Parameter(Mandatory=$true)][string]$ComputerName
To enhance the help file, utilize comment-based help. Add a detailed comment block to the top of the script:
<#
.SYNOPSIS
Displays the last startup time of a computer.
.DESCRIPTION
This script utilizes WMI to retrieve the time a computer last started.
.PARAMETER ComputerName
The name of the computer to query.
.EXAMPLE
Get-LastBootTime -ComputerName localhost
.LINK
www.howtogeek.com
#>
After these changes, the script should resemble the following.

Now, re-examine the help file.

The output is significantly improved! With the script now complete, final testing is required. To ensure consistency, exit the ISE and return to the PowerShell console.

By starting with a simple command and incrementally building upon it, proficiency will develop quickly. This concludes this session; we will continue with Geek School in the next installment.