Level 1
0 / 100 XP

PowerShell Splatting

PowerShell splatting is a method of passing a series of parameters to a command in a “single unit”. Splatting can make your code more human-readable and more accessible. In this lesson, we are going to look at how we can use splatting to simplify the code we use to create new Active Directory users - but keep in mind splatting can be used in any circumstance where you pass multiple parameters.

We are going to use the “Windows Server 2016 AD” lab from the IT Playground (link here). Launch the lab, log in to the Domain Controller and open the PowerShell ISE.

Once you’ve logged in, let’s take a look at what creating a new AD user account on a single line looks like:

New-ADUser -Name "Joe Friday" -GivenName “Joe” -Surname “Friday” -UserPrincipalName “joe.friday@serveracademy.com” -SamAccountName “joe.friday” -EmailAddress “joe@serveracademy.com” -Description “This is the users description” -OfficePhone “123-123-1234” -Path "OU=Domain Users,OU=ServerAcademy,DC=ServerAcademy,DC=local" -ChangePasswordAtLogon $true -AccountPassword $(ConvertTo-SecureString "Password!@#" -AsPlainText -Force) -Enabled $true

....Not very easy to read and definitely NOT easy to work with or modify at a later date. We could employ the use of backticks (`) to add each parameter on a new line. In PowerShell when you add the backtick, it allows you to continue the same command on a new line. You need to add a backtick for each new line that you want to add. It is much easier to read than a long one single line of code:

New-ADUser -Name "Joe Friday" `

Text
-GivenName “Joe” ` -Surname “Friday” ` -UserPrincipalName “joe.friday@serveracademy.com” ` -SamAccountName “joe.friday” ` -EmailAddress “joe@serveracademy.com” ` -Description “This is the users description” ` -OfficePhone “123-123-1234” ` -Path "OU=Domain Users,OU=ServerAcademy,D…