Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading