Powershell - item in string being interpreted as a command.

I'm trying to use powershell to run a command against another API.  When running the script, powershell see's the symdg command (which is intended for the API) and tries to do something with it, which it cannot.

#####
$command = "for /f "tokens=3" %a in ('symdg list -v ^|find /I "Group Name:"') do symdg delete %a -force"

Invoke-expression $command
#####

The following shows Powershell trying to interpret the command.

Unexpected token 'tokens=3" %a in (`'symdg list -v ^|find /I "Group' in expression or statement.
At C:\test1.ps1:3 char:68
+ $cmd03 =  "for /f "tokens=3" %a in ('symdg list -v ^|find /I "Group <<<<  Name:"') do symdg delete %a -force"
    + CategoryInfo          : ParserError: (tokens=3" %a in...|find /I "Group:String) [], ParseException
    + FullyQualifiedErrorId : UnexpectedToken

Is there a way to put the command into a string that powershell will not try to interpret it? I've tried to put the command into "<command>", ""<command>"", and other combinations of single, double quotes, brackets, parenthasies.
LVL 1
LN41Asked:
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:
"for" is a command that's built in to cmd.exe, so you would either need to start that to run it, or change your approach to not use it.  Maybe the below would work for you, but I'm really not sure about the quotes.
& cmd /c "for /f ""tokens=3"" %a in ('symdg list -v ^|find /I ""Group Name:""') do symdg delete %a -force"

Open in new window

0

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
Vaseem MohammedCommented:
See if this works, we need to include in single quote for all double quotes to be inside it and act as a part of string.

$command = 'for /f "tokens=3" %a in ("symdg list -v ^|find /I "Group Name:"") do symdg delete %a -force'
0
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
Microsoft Legacy OS

From novice to tech pro — start learning today.