Reduce typing

Caveat: When creating scripts, or when documenting commands that are used for others, it is recommended to not use aliases or positional parameters, or to shorten parameter names, as this leads to more difficult to understand code.

For further information see Best practice for using aliases in PowerShell scripts.

Tab completion

In PowerShell itself, tab completion will speed up typing commands, e.g. type ren then press tab twice, why twice?

If you haven’t typed enough characters to allow the command to be uniquely identified, presssing tab subsequent times will cycle through all the possible matching commands.

Typing rena then tab once would complete the command to rename-computer.

Skipping or shortening parameters

Positional parameters allow you to miss out the -parametername section, and you can also shorten the parameter name so that it is unique in the list of parameters (using the get-help [command] -full will list the full details of all parameters, allowing you to determine if a parameter is positional, and at what position, or whether it requires the name.

For the purpose of shortening parameter names, this command will also list all parameters, allowing you to determine what you can shorten the name to whilst still being unique within all possible parameters for the command).

Finding commands

Tip: There is a list of useful PowerShell commands at PowerShell one liners for common tasks.

If you are trying to do something that isn’t listed, you can search powershell for possible commands using itself, then use help [command] to find out more about commands.

Get-Command (gcm) & Get-Verb
gcm *printer* | more

would return all cmdlets that included the word printer. gcm is an alias for get-command. Other filters such as new* would return all commands that started with new.

If you notice a specific module in the returned list, you can filter by module name using the parameter -module [modulename] e.g.

gcm *printer* -module printmanagement | more

Similarly, get-verb will display what verbs (the first word in PowerShell cmdlets) are available.

Get-Help

Get-Help (or just help) can also be utilised by using it to search the help files, e.g. the following would search the help files for DNS and return commands whose help files included the word DNS.

Get-Help dns | more

To find out how to use a command, use:

Get-Help [command] | more

To see parameter information, you can add -detailed or -full, and to see examples, use -examples. To see the web page, use -online.

Tip: Importing the relevant modules or installing the relevant software will extend the help files present, e.g. installing the Windows RSAT tools also installs additional powershell commands & help onto a client desktop.

The following command will list the generic pages within PowerShell help, where you can find lots of additional useful PowerShell usage information, such as help about_aliases.

Get-Help about*

To ensure you have the latest version of help files, use the following command when you first start using PowerShell, and whenever enable additional windows features or install software (such as Windows RSAT) that come with their own PowerShell modules.

Via a PowerShell window that is run as administrator:

Update-Help -force

You may have to add -ea 0 to the above command if you encounter errors relating to UI cultures.

Objects & Pipelining

To view the properties (information) and methods (actions) of objects, you can use the get-member command. For example:

Get-Printer| Get-Member | more

You can use this when passing information or carrying out actions on objects, e.g. using the above you can see that PrinterStatus is a property, so you could use this in a filter:

Get-Printer | Where-Object PrinterStatus -eq Normal | Format-List Name,JobCount

When using get-member, take note of the Definition column, as this indicates what format is expected, e.g. numeric, string etc.

Formatting

You can choose to output the results from commands in various different formats.

Format-List (fl)

To output as a list, which typically returns more information, the command is format-list, or fl for short, For example, the following command outputs the results in a list (fl):

Get-Printer | Where-Object PrinterStatus -eq Normal | Format-List Name,JobCount
Format-Table (ft)

To output as a table, the command is format-table, ft for short.

Get-Printer | Where-Object PrinterStatus -eq Normal | Format-Table Name,JobCount

Additional parameters include -GroupBy (displays multiple tables), -AutoSize (the column widths), -Wrap (don’t truncate data, move to the next line). e.g.

Get-Printer | Format-Table Name,JobCount -AutoSize -Wrap -GroupBy PrinterStatus
Out-Gridview

Out-Gridview will output to a graphical window, where you have options for searching, filtering & sorting the output or selecting records (e.g. used in scripts), e.g.

Get-Printer | Out-GridView

How to run commands on remote systems

PowerShell remoting must be enabled to run some commands remotely, this is an advanced topic that will be covered in a future blog post. For further information see the MSDN invoke-command documentation.

Wrap the command with Invoke-Command, e.g.

Invoke-Command -ComputerName [computer] -ScriptBlock {[the powershell command]}

[computer] may be

  • a list of comma seperated computers,

  • a file pointing to a list of computers (using -type [filename], e.g invoke-command -computername -type c:\computers.txt -scriptblock…), or

  • an AD search result e.g. -invoke-command -computername (get-adcomputer -filter [your-filter].name) -scriptblock…

Testing what would happen first

Many action commands / cmdlets allow adding the -whatif parameter which will not perform the physical action, but will tell you what it would have done. If desire to test in this way, to check if the command includes this as an option, in PowerShell type help [command].