2

I am able to escape double quote character in PowerShell however when this includes strings with space escaping does not work correctly.

For example: I can translate the following ADUC to powershell

 Name in ADUC: cn="Jim" In powershell: "cn=\`"Jim\`"" 

How do I handle/escape the space between Jim and Carter as below ?

 Name in ADUC: cn="Jim Carter" In powershell: "cn=\`"Jim Carter\`"" The output I get is Jim, the space and string Carter are lost. 
8
  • Which PowerShell command is giving you problems because everything works for me IF you remove the backslashes? The backtick is the escape character and doesn't typically need to be escaped itself. Commented Apr 1, 2019 at 10:49
  • I have to escape the double quote before passing it to an external tool and having the spaces between the 2 strings does not work. It's like the space has to be escaped somehow Commented Apr 1, 2019 at 10:59
  • What is the escape character of your tool? If it is backslash and since PowerShell is okay with spaces in strings, can you just backslash escape the space? Commented Apr 1, 2019 at 11:25
  • 1
    Then can you try 'cn="Jim Carter"' ? By using single-quote characters around the whole string, the inner double quotes should be preserverd. Otherwise, try and figure out what the external tool uses as escape characters as @AdminOfThings commented. Commented Apr 1, 2019 at 11:57
  • 1
    If you are running the tool executable directly from PowerShell, you could possibly use the verbatim argument like tool.exe --% cn="Jim Carter". I can't be of much help without knowledge of the tool itself I think. Commented Apr 1, 2019 at 12:01

1 Answer 1

2

PowerShell has hidden re-quoting logic when arguments are passed to external utilities.

What you're seeing is bug in Windows PowerShell (that has since been fixed in PowerShell Core):

In short, "cn=\`"Jim Carter\`"" is passed to an external executable as cn=\"Jim Beam\", which unexpectedly turns into two arguments - note how the argument is not double-quoted as a whole, even though it needs to be.

Workarounds:

As part of a distinguished name in Active Directory, component values (such as Jim Beam for field cn) usually do not need double-quoting, even if they contain spaces. Therefore, perhaps the following will work (depending on the behavior of the executable you're invoking).

someExternalTool.exe "cn=Jim Beam" 

If you do need the executable to ultimately see string literal cn="Jim Beam", you must use --%, the stop-parsing symbol:

someExternalTool.exe --% "cn=\"Jim Beam\"" 

If you want the executable to see cn=\"Jim Beam\", i.e., with the " chars. \-escaped, use someExternalTool.exe --% "cn=\\\"Jim Beam\\\""

Note that use of --% then precludes the use of PowerShell variables as part of the same command.


In PowerShell Core, your original argument, "cn=\`"Jim Carter\`"" is passed as
"cn=\"Jim Carter\"" - i.e. with the necessary double-quoting as a whole - so the problem with the unexpected argument splitting wouldn't arise, and your target program would see cn="Jim Beam" after performing its own argument parsing.

If you need the target program to see cn=\"Jim Beam\", you'd have to use
"cn=\\\`"Jim Beam\\\`"" - or, using single-quoting,
'cn=\\\"Jim Beam\\\"'


As an aside: The need to \-escape " chars. to pass to external programs - in addition to PowerShell's own escaping - should itself be considered a bug, but it's a longstanding one that probably won't be fixed so as to maintain backward compatibility: see this GitHub issue.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the explanation and many suggestions. Doing this: someExternalTool.exe --% "cn=\"Jim Beam\"" works for me. I guess the executable accept it in this form cn="Jim Beam", with the " chars not escaped. It quite interesting how PowerShell interprets special characters when arguments are pass to external utilities
Thanks, @Tee, I've updated the answer to show the cn="Jim Beam" solution first. Yes, how PowerShell quotes arguments passed to external programs is unfortunate: you don't see what re-quoting is performed behind the scenes, you're forced to perform manual escaping of " chars., and there are bugs.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.