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
?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With