4

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Royce
  • 555
  • 1
  • 8
  • 14

2 Answers2

3

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

Community
  • 1
  • 1
Nacimota
  • 22,395
  • 6
  • 42
  • 44
  • i tried to use the inbuilt system but i'm only providing the script and have no influence how the parameters are formatted – Royce Mar 06 '14 at 09:53
  • 1
    +1 This is a good answer, more elaborate that mine (in terms of additional references and Powershell's script arguments functionality. I'll delete/replace my answer based on the additional requirement of not being able to modify input to script. – CJBS Mar 06 '14 at 18:04
  • 1
    @CJBS I didn't see your answer until after I had posted mine (I think we posted within a minute or two of each other). +1 to your new answer which I think is quite a clever approach the OP's problem. – Nacimota Mar 07 '14 at 00:42
  • how can i pass a string from command line and avoid powershell to do variable substitution... eg: show-string.ps1 \\pc2\(share1) if i enclose the arguments in single quotes works ok... but i do not want the user to worry about this... and powershell fails immediatly in line 1... does not give me the chace o '''work''' the arguments... – ZEE Jul 27 '20 at 23:05
2

The only way I can think of without being able to modify the input parameters would still require modifying the command called somewhat.

In order to preserve the quotes, perhaps try capturing the full command-line that the PowerShell script was called with.

So I put this in the test1.ps1 script:

Write-Host (Get-WmiObject -Query "select CommandLine from Win32_Process where CommandLine like '%test1%'").CommandLine

Then this is what is returned if the script is called in this manner:

PS C:\temp> powershell .\test1.ps1 a=application o="this object" msg_text="this is a text"
"C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe"  .\test1.ps1 a=application "o=this object" "msg_text=this is a text"

This is only possible if a new instance of PowerShell is called, however, otherwise the parameters won't be available by this method:

PS C:\temp> .\test1.ps1 a=application o="this object" msg_text="this is a text" > zzz3.txt

PS C:\temp>

Of course if this approach is taken, you'll then need to parse the input arguments manually, which you might be able to do with help of the $args object. It's a long-shot, but does preserve the quotes.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
CJBS
  • 15,147
  • 6
  • 86
  • 135
  • see comment above ;) unfortunately i have no influence how the parameters are formatted – Royce Mar 06 '14 at 09:53
  • I modified the answer to get the full command-line from WMI. This is a long-shot, but might put you in the right direction. – CJBS Mar 06 '14 at 18:13