2

I'm using Powershell(V4) and I'm following the code give here,however, it gives me an error when I run the code.

My Code:

[string]$zipPath="C:\Users\someUser\7z.exe"
[string]$parameters= 'a', '-tzip','C:\Users\someUser\Desktop\Archive.zip','C:\Users\someUser\Desktop\Test'

Powershell View:

PS C:\Users\someUser> $zipPath="C:\Users\someUser\7z.exe" $parameters= 'a', '-tzip','C:\Users\someUser\Desktop\Archive.zip','C:\Users\someUser\Desktop\Test' & $zipPath $parameters

& $zipPath $parameters

Output:

7-Zip (A) 9.20  Copyright (c) 1999-2010 Igor Pavlov  2010-11-18


 Error:
 Incorrect command line
0

2 Answers 2

1

Try using Start-Process with $parameters as the -ArgumentList:

Start-Process $zipPath -ArgumentList $parameters -wait
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, that worked as well. Thanks. Just out of curiosity, why didn't work the way the example showed?
It should've worked if you'd just had it as a normal string, but I guess & doesn't know how to parse an array to get program arguments, whereas Start-Process does.
0

That passes all of your arguments as a single string e.g.:

2> $ec = 'echoargs'
3> & $ec $parameters
Arg 0 is <a -tzip C:\Users\someUser\Desktop\Archive.zip C:\Users\someUser\Desktop\Test>

Command line:
"C:\Users\hillr\Documents\WindowsPowerShell\Modules\Pscx\Apps\EchoArgs.exe"  "a -tzip C:\Users\someUser\Desktop\Archive.
zip C:\Users\someUser\Desktop\Test"

Just pass your arguments normally:

4> & $ec a -tzip C:\Users\someUser\Desktop\Archive.zip C:\Users\someUser\Desktop\Test
Arg 0 is <a>
Arg 1 is <-tzip>
Arg 2 is <C:\Users\someUser\Desktop\Archive.zip>
Arg 3 is <C:\Users\someUser\Desktop\Test>

Command line:
"C:\Users\hillr\Documents\WindowsPowerShell\Modules\Pscx\Apps\EchoArgs.exe"  a -tzip C:\Users\someUser\Desktop\Archive.z
ip C:\Users\someUser\Desktop\Test

BTW echoargs is a tool from the PowerShell Community Extensions.

Comments