Configure PowerShell Cmdlet Default Values by Using Splatting

Configure PowerShell Cmdlet Default Values by Using Splatting

  • Comments 4
  • Likes

Summary: Learn how to use the Windows PowerShell splatting feature to avoid typing repetitive cmdlet parameters.

 

Hey, Scripting Guy! QuestionHey, Scripting Guy! I am wondering if you can help me. There are some cmdlets that have a lot of parameters. I need to fill these parameters out in order to cause the cmdlet to work properly, but once I have figured out the parameters, I would like to keep those parameters filled out, and not have to reenter them every time I want to use the cmdlet. For example, I like to configure the ping to be a certain size and generate a specific number of pings, but I hate to reenter all that data every time I want to ping a different machine. Is there something that can be done? I have thought about creating a custom function on the fly, but that seems like a lot of extra work.

—CC

 

Hey, Scripting Guy! AnswerHello CC,

Microsoft Scripting Guy, Ed Wilson, is here. Yeah, I know that it is the weekend, but it is the Scripting Wife’s birthday and we are heading out today. I thought I would answer a question really quick before we leave. CC, I know what you are talking about, and I like to use the up arrow and edit the previous command when doing repetitive operations. It works out really great, if I can remember to put the thing I need to change at the end. This is shown here:

Test-Connection -Count 1 -BufferSize 15 -Delay 1 -ComputerName localhost

Test-Connection -Count 1 -BufferSize 15 -Delay 1 -ComputerName loopback

The use of these commands and the associated results appear in the following figure.

Image of commands and associated results

This works okay, but I often forget to type the commands in exactly the right order, so I have to waste time editing the command line. Because of my font size, this also means the command wraps the line, and I have to scroll back and forth as I attempt to change the computer name. It is not a great solution.

Certainly, I can create a custom function, and make my desired parameters default values, but that is a bit of extra work. Unless I store the custom function in my profile or startup module, I will have to search for my function. Generally, I would just go ahead and fight it out at the command line before I could search to find my custom function.

A better approach is to use splatting. In splatting, I create a hash table of parameters and values, and then I supply that hash table to the cmdlet. The Windows PowerShell cmdlet is smart enough to look inside the hash table for the values to its parameters.

$pingConfig = @{

    "count" = 1

    "bufferSize" = 15

    "delay" = 1 }

Test-Connection localhost @pingConfig

When typing at the Windows PowerShell console prompt, I can put the entire hash table on a single line by separating the key/value pairs with a semicolon, as shown here:

$pingConfig = @{"count" = 1;"bufferSize" = 15;"delay" = 1}

The command and associated output are shown in the following figure.

Image of command and associated output

The nice thing about using splatting is that the order is not fixed. It is possible to put the hash table into the first to permit ease of changing the target computer. This is shown in the following figure.

Image of changing target computer 

CC, that is it. Using splatting, it is easy to configure your Windows PowerShell cmdlets to behave the way you want them to. Keep in mind that the keys for the hash table need to match the parameter names. The Get-Help cmdlet is invaluable for showing the parameters and detailing their use.

 

The Scripting Wife is ready to go, so I must run. Talk to you tomorrow when I begin a brand new week on the TechNet Script Center.  

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

 

 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Happy B-Day Scripting Wife! :)

    $Present = @{

    Type = 'Flowers'

    Size = 'Huge'

    Card = 'Happy Birthday ScriptingWife!'

    }

    Get-Present @Present | Send-Present -To ScriptingWife

    ;)

  • Thank you Bartek The flowers are beautiful.

  • I've never heard the term 'splatting' before. With that said, this is a great technique that I have not seen before either! This is great for repetitive tasks and will have to remember to include this in my bag of tricks :) Thanks for sharing.

  • @Craig Click on the article tag splatting at the bottom of this article. It will take you to about four or five Hey Scripting Guys articles that talk about and use splatting. It is a great feature.