How to use Hashtable splatting in PowerShell?

PowerShellMicrosoft TechnologiesSoftware & Coding

Splatting is the way to pass the collection of the parameters to the command as a single value. It uses the Hashtable splatting means that we can pass the Name and Value pair combination. We can use the named positional parameter for this with the values we want to provide.

For example,

First, we will check how we run the Copy-Item command here without splatting,

$params = @{
   Path = 'C:\Temp\25Aug2020.txt'
   Destination = 'C:\test1'
   Verbose = $true
   Force = $true
Copy-Item @params

Another Example,

$hash = @{
   From = ''
   To = ''
   SMTP = ''
   Priority = 'High'
Send-MailMessage @Hash -Subject 'Test Email'

In the above example, we are passing the splatted hash and separate Subject parameter in the command.

Published on 18-Jan-2021 11:51:01