Splatting is one of those things you can get used to real quickly!
Who hasn’t used the New-ADUser cmdlet? Just have a look at the syntax. Now imagine going through all those parameters on one line…
New-ADUser -Name "irwins" -GivenName "Irwin" -Surname "Strachan" -SamAccountName "irwins" -DisplayName "Irwin Strachan"
or separate lines using the tick ` character…
New-ADUser ` -Name "irwins" ` -GivenName "Irwin" ` -Surname "Strachan" ` -SamAccountName "irwins" ` -DisplayName "Irwin Strachan"
Granted this is readable until the tick looks like a dead pixel on your screen and you start scratching away at it (Why? First instinct I guess…) only to realize that it was indeed the tick character (Yes I’ve been there…)
Splatting makes for better (readable) code.
$prmNewADUser = @{ Name = "irwins" GivenName = "Irwin" Surname = "Strachan" SamAccountName = "irwins" DisplayName = "Irwin Strachan" } New-ADUser @prmNewADUser
Splatting also works with your functions. Go ahead try it! Neat huh?
Because it’s a hash table the add and remove methods are available which can make for some interesting tricks… I’ll discuss that one another time.
One thing though… When splatting the keys/parameters need to be known. For example:
$prmNewADUser = @{ Name= "irwins" GivenName = "Irwin" Surname = "Strachan" SamAccountName = "irwins" NickName = “Urv” DisplayName = "Irwin Strachan" } New-ADUser @prmNewADUser
This will fail because NickName isn’t a valid parameter. Suppose you’re using a csv file that contains NickName as a field. You could do the following:
$prmNewADUser.Remove(“NickName”)
right before running
New-ADUser @prmNewADUser
Ok so I got into it just a little… 🙂
Be sure to check out some free ebooks at powershell.org I enjoyed the gotcha’s ebook. If you’re serious about your powershell scripting skill there isn’t a shortage of good internet resources.
Hope it’s worth something to you…
Ttyl,
Urv