PowerShell Syntax Shorthand

If you’ve been introduced to PowerShell, you already know that the standard commandlets are rather lengthy. What you may not know is that many commands have built-in aliases that can be entered instead of the full syntax. For example, suppose you wanted to generate a list of running services. Using the standard syntax, you would enter:

Get-Service | Where-Object {$_.status -eq “running”}

The Get-Service commandlet has a built-in alias of “gsv” and the Where-Object commandlet can be shortened to “?”. Using the aliases results in the following script:

“gsv | ? {$_.status -eq “running”}”.

Not every commandlet has a built-in alias. To discover the list of commandlets that have built-in aliases, enter:

Get-Alias

For commandlets that do not have an alias, you can create your own user-defined aliases using the Set-Alias. For example, to create an alias for the New-ADOrganizationalUnit commandlet you could use the following syntax:

Set-Alias NewADOU New-ADOrganizationalUnit

Now, you can add new Active Directory Organizational Units using your alias. For example, to create a new OU called, “Finance” enter the following:

NewADOU-Name Finance

The name of the alias is entirely up to you. Keep in mind that using aliases can make PowerShell scripts harder to read, therefore, aliases should be well documented in your scripts using comments. As a best practice, define aliases at the beginning of your script, including the use of any built-in aliases.

#Commandlet aliases used in this script

# gsv = Get-Service (Built-in Alias)

# ? = Where-Object (Build-in Alias)

# User Defined Alias for New-ADOrganizationalUnit

Set-Alias NewADOU New-ADOrganizationalUnit

One last tip for shortening PowerShell syntax is understanding the commandlet parameters. First, in many cases, the parameter names can be omitted. This is because the parameters are ordinal and if the names are omitted, PowerShell expects the parameters in a specific order. For example, to add a new Organizational Unit named “Finance,” you could shorten the syntax as follows:

New-ADOrganizationalUnit Finance

Since Name is the first parameter in the New-ADOganizationalUnit commandlet, PowerShell assigns the value “Finance” to the Name parameter. Using the user-defined alias and omitting the parameter name results in the following shortened syntax for adding a new OU:

NewADOU Finance

PowerShell is a very user friendly and flexible scripting tool. As an additional aid to writing and saving PowerShell scripts, you can also use the PowerShell ISE, made available in PowerShell 2.0 and later versions. PowerShell ISE is a scripting interface that provides Intellisense and extensive help with commandlets and parameters. You can learn more about PowerShell scripting in Directions’ 10961: Automating Administration with Windows PowerShell class. Click Here to see the course outline.

For more information about Directions Training, please visit our website at www.directionstraining.com or call us toll free 855.575.8900!