Accepting Parameters without Quotes

by Mar 5, 2019

In the previous tip, we introduced a function that generates nicely centered headers and accepted a single string. Here is the function and its results:

function Show-Header($Text)
{
  $Width=80
  $padLeft = [int]($width / 2) + ($text.Length / 2)
  $text.PadLeft($padLeft, "=").PadRight($Width, "=")
}
 
PS> Show-Header Starting
====================================Starting====================================

PS> Show-Header "Processing Input Values"
=============================Processing Input Values============================
 

As you can see, the function works beautifully, but the user needs to quote the string once it contains spaces or other special characters. Wouldn’t it be nice if the function accepted strings without quotes as well, and treated all input as one argument?

That’s easily possible, provided:

  • the parameter is declared a clear data type, i.e. [string], so PowerShell knows what to do with your arguments
  • you are just using a single parameter, so PowerShell knows that all input needs to go into that parameter

Here is the revised function:

function Show-Header([Parameter(ValueFromRemainingArguments)][string]$Text)
{
  $Width=80
  $padLeft = [int]($width / 2) + ($text.Length / 2)
  $text.PadLeft($padLeft, "=").PadRight($Width, "=")
}

The magic is done by the ValueFromRemainingArguments attribute. Now, the user can simply type text and won’t need to use quotes:

 
PS> Show-Header Starting
====================================Starting====================================

PS> Show-Header Processing Input Values
=============================Processing Input Values============================ 
 

However, there is one caveat: any special characters such as parenthesis and quotes are interpreted and can still interfere. In these cases, you have to quote the string like before.

Your learning points:

  • use the ValueFromRemainingArguments attribute to allow PowerShell to assign all unbound (extra) arguments to that parameter
  • use distinct data types for parameters so PowerShell knows how to convert ambiguous data. Without a [string] data type, for example, PowerShell would have created a string array from your input values when a string contained spaces

psconf.eu – PowerShell Conference EU 2019 – June 4-7, Hannover Germany – visit www.psconf.eu There aren’t too many trainings around for experienced PowerShell scripters where you really still learn something new. But there’s one place you don’t want to miss: PowerShell Conference EU – with 40 renown international speakers including PowerShell team members and MVPs, plus 350 professional and creative PowerShell scripters. Registration is open at www.psconf.eu, and the full 3-track 4-days agenda becomes available soon. Once a year it’s just a smart move to come together, update know-how, learn about security and mitigations, and bring home fresh ideas and authoritative guidance. We’d sure love to see and hear from you!

Twitter This Tip! ReTweet this Tip!