Subscribe / Unsubscribe Enewsletters | Login | Register

Pencil Banner

The power of PowerShell: Essential tips Windows admins will love

Adam Bertram | Nov. 6, 2015
Make the most of Microsoft’s command line by mastering the nuances of the PowerShell language.

The power of PowerShell: Essential tips Windows admins will love
Credit: RyanMcGuire via pixabay

PowerShell can be a daunting tool to master for Windows admins used to working with the GUI. But as I discussed in our hands-on PowerShell intros for Windows Server and Exchange admins, adding even a little PowerShell into your daily mix can save a ton of effort.

Here, I’m going to concentrate on some of the fundamentals of PowerShell as a language to help you take your PowerShell skills to the next level. I won't cover technology-specific topics like how to manage Active Directory, Exchange, or IIS, nor will I cover specific PowerShell cmdlets. Instead, I will drill into the semantics of the language to show a few key techniques that you can put into your scripts today. Follow along and let's make your PowerShell scripts the best they can be.


PowerShell code is executed using commands such as cmdlets or functions. To increase their reuse value, commands typically employ parameters. Rather than requiring you to overspecify a custom function like Get-ContentFromFileXYZ.TXT and Get-ContentFromFileABC.TXT, PowerShell offers a Path parameter on its  Get-Content cmdlet. Parameters, like Path, are typically passed to functions and cmdlets with a dash, followed by the parameter name, a space, and the parameter argument or value:

Get-Content –Path C:\FileABC.txt

But cmdlets and functions like Get-Content often support several parameters, and before long, you may be typing commands like this:

Get-Content -Path C:\MyText.txt -ReadCount 1 -TotalCount 3 -Force -Delimiter "," -Filter '*' -Include * -Exclude 'a'

Here, eight parameters are being passed to Get-Content, and it’s clearly getting hard to read, to the point where confusion can arise in discerning the parameter from the value you're passing into the argument. Any time you use multiple parameters, it's wise to opt for another method of passing parameters to commands with the funny name of splatting.

Splatting, introduced in PowerShell Version 2, allows you to define all of your parameters up front in a much easier-to-read format.

Let's take that long, complicated set of parameters and see how all of them can be passed to Get-Content using splatting.

$getContentParameters = @{
   'Path'       = 'C:\MyText.txt'
   'ReadCount'  = 1
   'TotalCount' = 3
   'Force'      = $true
   'Delimiter'  = ','
   'Filter'     = '*'
   'Include'    = '*'
   'Exclude'    = 'a'
Get-Content @getContentParameters

Though I was forced to use more lines it's plain to see this method is much more readable than jamming them all on a single line. I also chose to line up the "=" statements by using tabs to have all of the parameter arguments lined up.


1  2  3  4  5  Next Page 

Sign up for Computerworld eNewsletters.