1

I'm using a third party tool to do some AD manipulation. I run a powershell script and pass arguments to it.

Everything works except if that argument contains an apostrophe in it like Jerry O'Connor. I've tried lots of different escape combinations without any luck.

Here is my command: script.ps1 -name "'%name%'" and name contain is Jerry O'Connor.

The error is

Result:The string is missing the terminator: '.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

I've tried:

  • script.ps1 -name "'%name%'"
  • script.ps1 -name \"%name%\"
  • script.ps1 -name ''name''

all with same error.

If you run this at the PS CMD level you'll see the error

powershell echo -name "'Jerry O'Connor'"

Anyone know how to pass an argument to script.ps1 -name "name" where that argument contains an apostrophe?

Cheers

mklement0
  • 382,024
  • 64
  • 607
  • 775
Mr80s
  • 65
  • 1
  • 8
  • As I was just cleaning up old comments and came across previous questions of yours, I wonder if it's time to revisit the fundamentals (see next comment) - though the baffling thing is that you have accepted _some_ answers to your previous questions, but I don't really see a pattern. Perhaps you can share your thought process? – mklement0 Nov 13 '21 at 02:22

2 Answers2

1

You need to escape any ' chars. inside the value of %name% as '' to make them work inside a single-quoted PowerShell string, which from cmd.exe you can do with %name:'=''%:

From cmd.exe / a batch file:

powershell.exe -Command ./script.ps1 -name "'%name:'=''%'"

If %name% contains verbatim Jerry O'Connor, the above expands to the following, which should work as intended:
powershell.exe -Command ./script.ps1 -name "'Jerry O''Connor'"


However, you can simplify quoting if you use the -File CLI parameter instead of -Command:

powershell.exe -File ./script.ps1 -name "%name%"

See also:

  • For guidance on when to use -Command vs. -File, see this answer

  • For a comprehensive overview of the PowerShell CLI, see this answer

mklement0
  • 382,024
  • 64
  • 607
  • 775
-1

Try the escape character as shown here:

$Test = "`'test`'"
$Test

enter image description here

Mogash
  • 130
  • 1
  • 7
  • @Mogash So if I'm passing the argument as a variable called name. How is it done? ie: `script.ps1 -name "`name`" ` as that didn't work when trying $test="`Jerry O'Connor`" – Mr80s Nov 10 '21 at 18:09
  • script.ps1 -Name "Jerry O`'Connor" – Mogash Nov 10 '21 at 18:23
  • `\``-escaping doesn't apply here, as what PowerShell ends up seeing - after command-line parsing when called from `cmd.exe`, which strips the `"` - is a _single_-quoted string, and in order to embed single quotes in such a string you have to _double_ them: `'Jerry O''Connor'` – mklement0 Nov 10 '21 at 18:24
  • 1
    Also, inside PowerShell `'` doesn't need escaping inside `"..."`; in your example `"'test'"` will do. – mklement0 Nov 10 '21 at 20:43
  • 1
    I see I did a bummer on my previous comments. @mklement0 is right. – Mogash Nov 10 '21 at 20:54