PowerShell Tutorial - Three Key Commands
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
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
To filter the list, employ the famous star * wildcard; here are three examples:
Let us experiment with a variation of this wildcard theme which
displays only cmdlets beginning with the verb 'set':
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
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-PSProvider (Or plain: PSProvider)
This is what happened when I typed just: Get-Psprovider <carriage return>
Name Capabilities Drives
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.
Another Command PSSnapin
What PSSnapin does is reveal the sources for the built-In cmdlets:
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:
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
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:
Guy's List of Useful Information Revealed by Get-Help