olly - Fotolia

Manage Learn to apply best practices and optimize your operations.

Build your PowerShell command cheat sheet with these basics

Even if you've previously used PowerShell, it's beneficial to add new commands to your reference sheet and keep your overall knowledge as current as possible.

You've probably dabbled in PowerShell, but there are some tips that can help make scripting easier and quicker. If you build up a PowerShell command cheat sheet, you can reduce the need to memorize every command.

First, you must know how to use the appropriate PowerShell syntax to program effectively. There are many ways to learn PowerShell besides trial-and-error coding. Aside from Microsoft's training series, there are also YouTube seminars, books by industry experts, blogs, community events and online script examples.

Use PowerShell functions, hash tables and the pipeline

Start with PowerShell functions. It is easier to produce several small programs instead of one big program. Functions take a specific object, do a specific task and deliver a specific object. Each bit of code has one specific purpose and does it well, which makes it easy for you to complete specific tasks.

For example, this function calculates tax on a specific item based on its initial price and VAT:

function Output-TaxPayable {
 param( [int]$basecost, [int]$VAT )
 $basecost + $VAT

PS C\SCRIPTS\:\> Output-TaxPayable -price 100 -tax 20

Use PowerShell PSObjects and hash tables. These functions do the heavy lifting when you need to index or sort multivalued data items.

Use the PowerShell pipeline. The PowerShell pipeline -- the mechanism to move data and results through cmdlets -- has forward processing built in. Think of a factory production line: When used correctly, each command in the pipeline can handle a separate item in the data stream and organize the work. It looks like:

PS C:\SCRIPTS\get-vm | Select Name, NumCPU

"The pipeline |" takes the get-vm output, which includes several fields of data, and combines it for a streamlined data view, making it a good addition to your PowerShell command cheat sheet.

PowerShell cmdlets
You can implement these cmdlets for basic PowerShell navigation and data recall.

Take advantage of the help function. Getting good examples of PowerShell code can be difficult. The built-in get-help command enables users to find code samples easily.

To get code samples, you can use the -examples switch. This function is written as:

Get-help get-vm -examples.

PowerShell for remote sessions, management tasks

Gain parallel processing with PowerShell jobs, runspaces and remote sessions. The local PowerShell session has a default of one runspace, and it can run one foreground with many background scripts. You can also create runspaces that allow for higher capacity and more rapid processing.

Additionally, remote PowerShell sessions enable parallel processing across multiple computers, and they allow a script to perform multiple actions in different places at the same time.

To set up a remote session, use the following code line from your PowerShell command cheat sheet:

C:\SCRIPTS\:> $MyServer = New-PSSession -ComputerName $MyServer

At this point, the $MyServer variable is ready to use remote sessions as needed.

Take advantage of reflection assemblies, DLLs and application command shells. PowerShell can access DLLs through a reflection assembly; this is tricky to use, but it exposes most OS and application features to your script. If your managed application has a PowerShell command shell, learning the commands will make the application features more accessible to the script.

Use Windows Management Instrumentation (WMI). WMI and its base -- the common information model -- give scripts easy access to compliant operating systems. The main code is:

Get-WmiObject Win32_Computersystem

This function enables the user to expose and use all the various WMI objects that the system makes available.

WMI PowerShell commands
This chart illustrates some of the main functions of WMI in PowerShell.

Use PowerShell Secure Shell (SSH) and REST to talk to non-Windows systems. SSH offers access to a device's command-line interface and REST connects to a device's web interface. Using a native PowerShell environment with a simple network management protocol is a stretch -- both SSH and REST make the process easier.

Expand your PowerShell command cheat sheet

PowerShell authors have embraced open source code, which means the use of published code is an incredible compliment. When you implement prewritten code, the author gets recognition and you benefit from their experience.

PowerShell 5 introduced a useful feature called transcripting. It formats a script's textual output so it is simple to select and debug or troubleshoot the system as needed. An example is:

PS C:\SCRIPTS\> start-transcript -path output.txt

Once done, everything will be captured and logged to the text file.

This list is far from exhaustive, but hopefully it gives you some guidance and helps build a mature PowerShell command cheat sheet where you can refine reusable code over time.

This was last published in June 2018

Dig Deeper on Best practices for data center operations

Join the conversation


Send me notifications when other members comment.

Please create a username to comment.

Do you have a cheat sheet for PowerShell commands you use often? Which commands made the list?
Transcription was not introduced by PS5. Start-transcript was in Powershell as of version 1... See https://social.technet.microsoft.com/wiki/contents/articles/13769.powershell-1-0-cmdlets.aspx