The awesome power of Microsoft PowerShell scripts

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.

Before PowerShell, the primary scripting language to perform Windows operational tasks was Visual Basic Scripting. Though Visual Basic Scripting still exists and many IT admins still prefer to use VB scripting, it is worth mentioning that PowerShell reduces the code complexity. For example, to check the services on a Windows computer, you will need to write the VBScript code in a notepad, save the file as .VBS and then execute it using the CSCript host execution file. But by using PowerShell you can use a simple Get-Service cmdlet that can help you list services from a Windows computer quickly. If you needed to save the output to a file using VB script, you will again need to write a bunch of code in the VB script, which involves opening the fil, and then writing the contents to the file. PowerShell just doesn’t require you to open the file before contents retrieved by a PowerShell cmdlet can be written to a file. All you need to do use “Export-CSV” or “Add-Content” cmdlets.

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

Three types of PowerShell operations

PowerShell scripts
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=TechGenix,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 JohnThomas –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 JohnThomas –HomeFolder \\Server1\%UserName%

The commands below set mail property for user JohnThomas.

$ThisUser = Get-ADUser –Identity JohnThomas –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 JohnThomas

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.

Saving information in a CSV file

When it comes to saving information in a CSV file, PowerShell saves a lot of scripting work. PowerShell provides “Export-CSV” cmdlet that you can use with any other PowerShell cmdlets. By default, any PowerShell command that you execute shows the resulting information in the PowerShell command window unless you add Export-CSV to the end of the command as shown in the command below:

Get-ADUser –Filter * -SearchBase “OU=TestUsers,DC=TechGenix,DC=Com” | Export-CSV C:\Temp\AllUsersInAnOU.CSV

Notice that in the above command, you are just adding “Export-CSV” at the end of the command to have the output exported in a CSV file.

As you can see, PowerShell scripts reduce the time it takes to perform daily operational tasks. PowerShell is capable of performing three operational tasks; Get, Set, and Remove and 97 percent of PowerShell cmdlets start with these three words to easily identify the function of a PowerShell cmdlet. We also explained how easy it is to combine two cmdlets in a single command to make your PowerShell scripts more efficient.

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top