Introduction to Key PowerShell Commands
Here are three simple, yet key commands, which are designed to get you started with PowerShell. As you study, my example scripts keep in mind the golden rule, Verb-Noun, for example, Get-PSProvider.
Three Key PowerShell Commands or Cmdlets
♣
1. PowerShell Get-Command
Let us begin this PowerShell scripting tutorial by testing Get-Command. What this instruction does is list all of PowerShell’s noun-verb pairs; incidentally, these are also referred to as built-in cmdlets. Assuming that you have opened a PowerShell session, then you should see a prompt like: PS > Now type just one hyphenated phrase:
# Windows PowerShell Tutorial
Get-Command
To filter the list, employ the famous star * wildcard; here are three examples:
Get-Command out*
Get-Command add*
Get-Command Get-*
Let us experiment with a variation of this wildcard theme which displays only cmdlets beginning with the verb ‘set’:
Get-Command set* -commandType cmdlet
It is possible to tweak the display of your output columns with ft (Format-Table). My hidden agenda here is to give you practice with PowerShell’s Pipe symbol (|), try:
#PowerShell Scripting Tutorial
Get-Command | Format-Table name, definition -auto
At the moment we are just ‘playing’, testing, or feeling our way, thus do feel free to experiment with your own variations of my suggestions. Once you have seen the long list of all possible commands, chose one cmdlet for further research, for example:
Refine Get-Command with -CommandType
# PowerShell WhatIf cmdlets
Get-Command -commandType cmdlet `
| where { $_.parameters.keys -Contains "WhatIf"}
Get-PSProvider (Or plain: PSProvider)
This is what happened when I typed just: Get-Psprovider <carriage return>
Name Capabilities Drives
—- ———— ——
Alias ShouldProcess {Alias}
Environment ShouldProcess {Env}
FileSystem Filter, ShouldProcess {C, D, E, H…}
Function ShouldProcess {Function}
Registry ShouldProcess {HKLM, HKCU}
Variable ShouldProcess {Variable}
Certificate ShouldProcess {cert}
Challenge: try
PSProvider | Get-Member
Guy Recommends: Free WMI Monitor for PowerShell
Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems. Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.
Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory, or Exchange Server. Give this WMI monitor a try – it’s free.
Download your free copy of WMI Monitor
Another Command PSSnapin
What PSSnapin does is reveal the sources for the built-in cmdlets:
Clear-Host
Get-PSSnapin
# Or
Get-PSSnapin |ft name, description -autosize
Note how every PowerShell noun is singular, PSSnapin, Command, PSProvider. Also note how a Pipe (|) followed by ft means format the output as a table, as opposed to format the results as a list (fl). Any words which follow ‘ft’ are names of the fields, each separated by a comma. At the end of the above command is the switch -autosize, this extra parameter tells PowerShell to close-up the width of the columns. When ever you use Format-Table, or ft, try appending -autosize, or the shorter version: -auto.
In the example below, I have used ft to omit the Description field and just displayed the name:
Get-PSSnapin |ft name:
Name
—-
Microsoft.PowerShell.Core
Microsoft.PowerShell.Host
Microsoft.PowerShell.Management
Microsoft.PowerShell.Security
Microsoft.PowerShell.Utility
More Windows PowerShell Tutorial Commands
2. PowerShell Get-Help
Avoid arrogance, put aside pride, and call for PowerShell’s built-in help. We all have to learn somewhere and only you know what you type in the privacy of your PowerShell command line.
Perhaps what puts us off trying built-in help is a bad experience with the stilted help of an old DOS system. Dare I suggest that experience with internet search techniques makes us more willing to try a modern application’s own help?
PowerShell’s help has some interesting switches, such as: -full and -examples. Incidentally, -exampleS also works, a rare case of a plural PowerShell noun.
# Key Windows PowerShell Command
Clear-Host
Get-Help Get-Wmiobject -full
Note: Get-Help does not require the pipe symbol. In fact, the pipe (|) only gets in the way of Get-Help; PowerShell does its best to interpret:
Get-Help | Get-WmiObject cim_chip But it still results in an error, therefore stick with plain simple:
Get-Help Get-WmiObject
Guy’s List of Useful Information Revealed by Get-Help
®