PowerShell and Ampersand

I have the following simple Powershell script:

$test = "Test%$&*()!@#&"

Write-Host  "$test" 
  
C:\Temp\Test.cmd $test

Open in new window


Test.cmd:
ECHO Parameter = %1

Output:
PS H:\> $test = "Test%$&*()!@#&"

Write-Host  "$test"
 
 
C:\Temp\Test.cmd $test


Test%$&*()!@#&

H:\>ECHO Parameter = Test%$
Parameter = Test%$
Test.cmd : '*' is not recognized as an internal or external command,
At line:6 char:1
+ C:\Temp\Test.cmd $test
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: ('*' is not reco...ternal command,:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
 
operable program or batch file.

PS H:\>
LVL 96
Bob LearnedAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
I don't think this is anything to do with PowerShell.
At a command prompt (not PS), try running
test.cmd Test%$&*()!@#&

Open in new window

and you see the error.  I'd say it's a limitation of the command processor, but that's just a guess.
aikimarkCommented:
You might need to escape the & character.  My first thought would be to try the backslash or carrot character to do the escape.

Otherwise, you might try putting the string in quotes.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
I too thought it to be a cmd issue, but the error message looks like it is generated by PowerShell, not cmd.exe, and that puzzled me. However, it is indeed cmd.exe.
The command is expanded to be
C:\Temp\Test.cmd Test%$&*()!@#&

Open in new window

in PowerShell. Though you may not use that line directly in PowerShell (the ampersand is an reserved operator, and may not be used literally outside of a string), it is valid if using a string var.
Now, because cmd.exe is the registered app for .cmd files, it is triggered and starts interpreting the command line. It sees
C:\Temp\Test.cmd Test%$ & *()!@# &

Open in new window

because ampersand is a command separator (like semi-colon in PowerShell), used for concatenating commands, like in
echo one & echo two

Open in new window

The batch file runs fine, but then a command starting with * is tried to be run - to no avail, of course. Even if that would succeed, the trailing ampersand would cause an error, as no further command is following it.
You wiill have issues with parens, caret, ampersand and potentially percent and exclamation mark if trying to pass to a batch file. As long as there is no double quote involved, this should work in PowerShell
C:\Temp\Test.cmd """$test"""

Open in new window

but the double quotes are then part of the parameter:
Parameter = "Test%$&*()!@#&"

Open in new window

This again can be compensated by using %~1, but that again opens another can of worms with the same result - an error message because of the ampersand, but this time from within the batch file.

Base line: You are f*cked up if you need to call .cmd files with parameters or environment vars containing some special characters. I can tell you a thing or two about it - trying to pass passwords that way can be pain.

My recommendation is to stay within PowerShell. Mixing PS and VBS is ok (if necessary), but never call .cmd files; the mix makes it just more difficult to maintain and troubleshoot.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bob LearnedAuthor Commented:
It was the triple double-quotes that fixed my problem:

C:\Temp\Test.cmd """$test"""
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.