How to send an input phrase without using quotes

I would like to call a script like so:

ask.ps1 what is foo

as opposed to

ask.ps1 "what is foo"

and then have the "ask.ps1" script treat all the words (which can be a random number of words) as one string

ask.ps1


param(
     $inputString = <???>
)
write-host ("Received input: " + $inputString)

is this possible?

>Solution :

Both options are valid, either use the automatic variable $args:

& { Write-Host ("Received input: " + $args) } foo bar baz

Or a single parameter taking ValueFromRemainingArguments:

& {
    param([Parameter(ValueFromRemainingArguments)] $hey)
    
    Write-Host ("Received input: " + $hey)
} foo bar baz

In both cases the array is coerced to a string and joined by $OFS (a space by default).

Leave a Reply