How To: Useful PowerShell scripts/Commands

Microsoft PowerShell is a great scripting platform. Microsoft provides PowerShell support for almost all Windows Server roles and features. You can use PowerShell to automate repeated tasks as well as perform tasks easily using the command line that you usually perform using the GUI tools. For example, in a production environment if you need to check patching status on Windows servers, using the PowerShell scripting approach is much easier than connecting to each server individually and then checking the patching status. I am a regular user of PowerShell and have designed thousands of PowerShell scripts in my 18 years of IT experience.

Cmdlets: The building blocks of PowerShell scripts
PowerShell offers cmdlets (pronounced “command-lets”), sometimes referred to as command line tools, which you can use to get information about the operating system, applications installed on a Windows computer, network information, patching status on Windows computer, and much more. It is important to note PowerShell cmdlets are written in .NET and use .NET classes to perform required tasks. If you wish to find out which PowerShell cmdlets are available on the local computer, execute this command:


Get-Command is a PowerShell cmdlet that retrieves cmdlets, functions, filters, and PowerShell scripts installed on the local computer. The above command queries all the PowerShell modules installed on the local computer. If you need to see all commands installed on the local computer in an order, execute this command:

Get-Command –Type cmdlet | Sort-Object –Property Noun |FT –GroupBy Noun

Using PowerShell you can perform three types of operations. They are Get, Set, and Remove. Any PowerShell cmdlet that starts with “Get” is typically used to retrieve information from Windows components. For example, Get-ADUser performs a read operation against the Active Directory database to retrieve the user information. A few examples of using Get-ADUser PowerShell cmdlet are shown below:

Get-ADUser –Filter * -SearchBase “OU=TestUsers,DC=pinnaclenetworking,DC=Com”

The above command retrieves all user accounts located in an organizational unit whereas the command below retrieves properties associated with a single Active Directory user:

Get-ADUser –Identity ArtEppley –Properties *

PowerShell cmdlets that start with “Set” are used to edit Windows component information. For example, by using Set-ADUser you can various operational tasks as shown in the commands below:

Set-ADUser –Identity ArtEppley –HomeFolder \\Server1\%UserName%

The commands below set mail property for user ArtEppley.

$ThisUser = Get-ADUser –Identity ArtEppley –Properties Mail
$ThisUser.Mail = [email protected]
Set-ADUser -Instance $ThisUser

powershell scripts

Any PowerShell cmdlets that start with “Remove” are used to perform deletion operations. For example, Remove-ADUser can be used to remove an Active Directory user as shown below:

Remove-ADUser –Identity ArtEppley

Combining PowerShell cmdlets
One of the best features PowerShell provides is the ability to combine two or more PowerShell cmdlets in a single PowerShell command. Such commands are sometimes called one-liner PowerShell commands. For example, if you need to remove all disabled user accounts from Active Directory, you can combine Search-ADAccount and Remove-ADUser PowerShell cmdlets as shown in the command below:

Search-ADAccount –AccountDisabled | Where {$_.ObjectClass –eq “User”} | Remove-ADUser

As you can see in the above command, Search-ADAccount PowerShell cmdlet performs a search throughout active directory to look for disabled accounts and then by using the pipe (|) operator you are instructing the Remove-ADUser to perform a deletion operation only on the users that are retrieved by the Search-ADAccount.

PowerShell aliases
Though many PowerShell cmdlets are easy to read and use, PowerShell also provides a feature called PowerShell alias. PowerShell is smart enough to complete the name of a PowerShell cmdlet with a press of Tab key. In case you have difficulty remembering the name of the PowerShell cmdlets, you can create PowerShell aliases. For example, you can create an alias for the Get-MailBoxStatistics PowerShell cmdlet using the command below:

New-Alias ShowMB Get-MailBoxStatistics

When you type “ShowMB” in the PowerShell window, PowerShell looks for the cmdlet associated with the ShowMB alias and then execute it.