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

Passwords Haven’t Disappeared Yet

123456. Qwerty. Iloveyou. No, these are not exercises for people who are brand new to typing. Shockingly, they are among the most common passwords that end users choose in 2021. Research has found that the average business user must manually type out, or copy/paste, the credentials to 154 websites per month. We repeatedly got one question that surprised us: “Why would I ever trust a third party with control of my network?

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.
13 Email Threat Types to Know About Right Now

As email threats evolve and multiply, keeping track of them all—and staying protected against the many different types—becomes a complex challenge. Today, that requires more than just the traditional email gateway solution that used to be good enough.

In this eBook you will learn:

  • What are the most common and challenging email attacks for organizations?
  • How to defend against sophisticated email threats, such as spoofing, social engineering, and fraud
  • How to protect employees at the inbox level with the right technologies and security-awareness training
  • How to use a multilayered protection strategy to reduce susceptibility to email attacks and better defend your business and employees

Sponsored by: