Learn What IT Pros Need to Know About Windows 11 - August 26th at 1 PM ET! Learn What IT Pros Need to Know About Windows 11 - August 26th at 1 PM ET!
PowerShell

How to Use PowerShell Splatting to Make Code More Readable

There are a couple of ways you can make your PowerShell code more readable. In this article, I’m going to show you how to use splatting with the New-ADUser cmdlet to do just that, make your code more digestible. For yourself and for others to read.

Splatting can be used with any PowerShell cmdlet that accepts parameters. But in this example, we’ll stick to New-ADUser as it’s a cmdlet most Windows system administrators are familiar with. Here’s an example of a typical New-ADUser command. It’s long, difficult to read, and it never seems to end.


New-ADUser -Name 'Russell Smith' -Enabled $true –GivenName Russell –Surname Smith -Accountpassword (ConvertTo-SecureString PassW0rd! -AsPlainText -Force) -Path:"OU=Accounts,DC=ad,DC=globomantics,DC=uk" -ChangePasswordAtLogon $true -SamAccountName russellsmith –UserPrincipalName [email protected] -City London -Department Infotech

Using backticks for line continuation in PowerShell

One way that you can make the above command easier to read is using backticks. You often see this method used in documentation or script examples. Each line of code ends with a backtick if it will be followed by another line of code. I.e. the final line of code in the command doesn’t require a backtick.

New-ADUser -Name 'Russell Smith' -Enabled $true –GivenName Russell `
–Surname Smith -Accountpassword (ConvertTo-SecureString PassW0rd! -AsPlainText -Force) `
-Path:"OU=Accounts,DC=ad,DC=globomantics,DC=uk" -ChangePasswordAtLogon $true `
-SamAccountName russellsmith –UserPrincipalName [email protected] `
-City London -Department Infotech

Backticks can be problematic when copying and pasting code. If they end up out of place, the code will not work as expected. And some people consider use of backticks bad practice.

Sponsored Content

Say Goodbye to Traditional PC Lifecycle Management

Traditional IT tools, including Microsoft SCCM, Ghost Solution Suite, and KACE, often require considerable custom configurations by T3 technicians (an expensive and often elusive IT resource) to enable management of a hybrid onsite + remote workforce. In many cases, even with the best resources, organizations are finding that these on-premise tools simply cannot support remote endpoints consistently and reliably due to infrastructure limitations.

PowerShell Splatting

The best way to make the command shorter and easier to read is using splatting. Define the parameters you want to use in a separate code block and then pass them to the cmdlet as shown below. Each parameter is separated using a semicolon.

$params = @{
    Name = 'Russell Smith';
    Enabled = $true;
    GivenName = 'Russell';
    Surname = 'Smith';
    Accountpassword = (ConvertTo-SecureString PassW0rd! -AsPlainText -Force);
    Path = "OU=Accounts,DC=ad,DC=globomantics,DC=uk";
    ChangePasswordAtLogon = $true;
    SamAccountName = 'russellsmith';
    UserPrincipalName = '[email protected]';
    City = 'London';
    Department = 'Infotech';
  }

New-ADUser @params

The disadvantage of splatting is that you can’t use tab completion to determine what parameters can be used with a PowerShell cmdlet. So, if you are not sure what parameters to use, you might want to start by writing your command in the traditional long-form method as shown at the beginning of this article and then reformat it to use splatting.

Image #1 Expand
Use PowerShell Splatting to Make Code More Readable (Image Credit: Russell Smith)

 

It’s also worth noting that if you decide to use splatting, you’ll need to enclose parameter values in single quotes regardless of whether they include a space. For example, -Name ‘Russell Smith’ must always be enclosed with single quotes because it contains a space.

In a traditional PowerShell command, -GivenName Russell doesn’t need to be enclosed because there is no space in the string. But when using splatting, strings must always be enclosed.

 

Related Topics:

BECOME A PETRI MEMBER:

Don't have a login but want to join the conversation? Sign up for a Petri Account

Register
Comments (0)

Leave a Reply

IT consultant, Contributing Editor @PetriFeed, and trainer @Pluralsight. All about Microsoft, Office 365, Azure, and Windows Server.

Register for Advanced Microsoft 365 Day!

GET-IT: Advanced Microsoft 365 1-Day Virtual Conference - Live August 24th!

Join us on Tuesday, August 24th and hear from Microsoft MVPs and industry experts about how to take advantage of Microsoft 365 at a technical level and dive deep into the features and functionality that will make your environment more secure and compliant.

RSVP Now

Sponsored By