How PowerShell Splatting Works
Splatting is more than just a neat way of formatting cmdlets with multiple parameters, it teaches us about PowerShell functions and hashtables.
Topics for PowerShell’s Functions
- Introduction to PowerShell Splatting
- Troubleshooting PowerShell Splatting
- The Problem that PowerShell Splatting Cures
- Splatting and WMI Queries
♣
Introduction to PowerShell Splatting
The ‘problem’ that splatting solves is untidy formatting. It can be difficult to troubleshoot scripts with long lines containing multiple parameters. While you can format with (`) backticks, they can introduce as many problems as they solve.
PowerShell Splatting Example
I will use writing to the eventlog as a vehicle for testing PowerShell’s splatting.
Concept 1: Create a function to hold the parameters; I choose $MyLog.
Concept 2: Build a @hashtable; containing multiple: Parameter = "Value".
Concept 3: Splat with cmdlet thus; Write-Eventlog @MyLog.
# PowerShell Splatting example:
Clear-Host
$MyLog = @{
ComputerName = "LocalHost"
Logname = "Windows PowerShell"
Source = "PowerShell"
EventID = "600"
EntryType = "Information"
Message ="Guy is at work."
}
Write-Eventlog @MyLog
# Optional command to view the new EventID
Get-EventLog -LogName "Windows PowerShell" -Newest 10
Note 1 Crucial Point: While the function is called $MyLog, when you "splat" substitute @ for $, [@MyLog not $MyLog] and in this way unleash the hashtable’s Key = ‘Value’ pairs.
Note 2: The main engine in the above example is the cmdlet is Write-Eventlog.
Guy Recommends: Free WMI Monitor for PowerShell
Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems. Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.
Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory, or Exchange Server. Give this WMI monitor a try – it’s free.
Download your free copy of WMI Monitor
Troubleshooting PowerShell Splatting
Speech Marks or "Quotes"
Just a reminder that PowerShell interprets a "double" speech marks in command mode and expands any variables, whereas ‘single’ speech marks means treat the contents literally. The upshot is that mostly you only need one set of quotes ; my advice is start with double quotes, and only use nested sets as a last resort.
PowerShell Splatting Formatting
One of the rationales for using splatting is to avoid the (`) backtick. A half-way house is to employ the (;) semi-colon between key = value pairs.
To my thinking, using semi-colons destroys the purpose of the splatting idea. The only reason that I included this example is because it illustrates how you can have more than one Key = ‘Value’ pair on one line.
Clear-Host
$MyWork = @{
ComputerName = "LocalHost"; Logname = "Windows PowerShell"
Source = "PowerShell"; EventID = "600"
EntryType = "Information"; Message ="Guy is not at play."
}
Write-Eventlog @MyWork
Get-EventLog -LogName "Windows PowerShell" -Newest 10
The Problem that PowerShell Splatting Cures
What can happen with the (`) is that you leave a space after this punctuation, which causes the script to fail. Try adding a space after the second line. You don’t get this problem with PowerShell splatting.
Clear-Host
Write-Eventlog -ComputerName "LocalHost" `
-LogName "Windows PowerShell" -Source "PowerShell" `
-EventID "600" -EntryType "Information" -Message "Guy working"
Get-EventLog -LogName "Windows PowerShell" -Newest 10
Note 3: To see the problem try again, but this time with white space after the `
-LogName "Windows PowerShell" -Source "PowerShell" ` (white space)
Note 4: Another problem with ` is that it can be difficult to see, and easy to overlook.
Guy Recommends: A Free Trial of the Network Performance Monitor (NPM) v11.5
SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network. This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.
What I like best is the way NPM suggests solutions to network problems. Its also has the ability to monitor the health of individual VMware virtual machines. If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.
Download a free trial of Solarwinds’ Network Performance Monitor
Splatting and WMI Queries
Here is an example where we need to query disk partitions, observe how we employ a simple hashtable.
# PowerShell Splatting with WMI
Clear-Host
$Partition = @{Query="Select * from Win32_DiskPartition"}
Get-WmiObject @Partition | Format-Table -AutoSize
Note 5: This is a simple example; WMI splatting comes into its own where you have multiple queries.
Summary of PowerShell Splatting
Even if you eliminate white space around the backticks, PowerShell scripts with (`) are just not as elegant as those with splatting. Another benefit of this technique is that it gives you an excuse to employ more advanced techniques such as hashtables and functions.
If you like this page then please share it with your friends
See more Microsoft PowerShell output tutorials:
• PShell Home • Out-GridView • PowerShell Splatting • Read-Host • Write-Host • ConvertTo-Html
• Export-CSV • Import-CSV • PowerShell Write-Progress • PowerShell Measure-Command
• PowerShell 3 -PassThru • PowerShell 3.0 Redirection • ConvertFrom-Csv • Free CSV Import Tool
Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.