Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep quotes in $args using PowerShell

Is it possible to keep the quotes in $args?

The script is called like this:

.\test1.ps1 a=application o="this object" msg_text="this is a text"

The quotes are mandatory for further processing. Is there a way to keep them inside $args?

like image 854
user3322838 Avatar asked Aug 31 '25 18:08

user3322838


1 Answers

You can escape quotes in PowerShell with a backtick:

.\test1.ps1 a=application o="`"this object`"" msg_text="`"this is a text`""

You can also nest double quotes inside single quotes (or vice versa) but beware that variables are not evaluated in a string delimited with single quotes.

.\test1.ps1 a=application o='"this object"' msg_text='"this is a text"'

Additionally, why use $args at all? Why not use the massively powerful inbuilt parameter system?

Further reading:

Get-Help About_Quoting_Rules

Stack Overflow - How to Handle Command Line Arguments in PowerShell

TechNet Magazine - Windows PowerShell: Defining Parameters

like image 122
Nacimota Avatar answered Sep 02 '25 21:09

Nacimota