191

I have a command that I have build and stored in a variable in PowerShell. This command works if I do a Write-Host and copy and paste into a standard cmd.exe window.

How do I execute this command from inside my script?

I have tried several combination of Invoke-Command or Invoke-Expression with no luck.

This is how I built the variable:

$cmd1 = $arcprg + $arcdir + "\" + $site1 + "-" + $hst + "-" + $yesterday + ".zip " + $logpath1 + "u_ex" + $yesterday + ".log"

This is what the variable looks like if it is printed to the screen:

7z.exe a -tzip c:\arc_logs\site-host-at-web1-100827.zip c:\inetpub\logs\logfiles\w3svc1\u_ex100827.log
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Travis
  • 3,073
  • 6
  • 28
  • 22
  • isn't this just the '&' symbol? https://stackoverflow.com/questions/22074507/what-does-the-symbol-in-powershell-mean/22074638 – john k Jul 08 '21 at 14:27
  • Perfectly valid to build up a command to run an external program such as 7z. But however one does this in a general case, one will want to be sure not to blindly use parameters that come from potentially untrusted sources that could cause a malicious action to be performed. – wojtow Oct 30 '22 at 06:20

2 Answers2

262

Here is yet another way without Invoke-Expression but with two variables (command:string and parameters:array). It works fine for me. Assume 7z.exe is in the system path.

$cmd = '7z.exe'
$prm = 'a', '-tzip', 'c:\temp\with space\test1.zip', 'C:\TEMP\with space\changelog'

& $cmd $prm

If the command is known (7z.exe) and only parameters are variable then this will do

$prm = 'a', '-tzip', 'c:\temp\with space\test1.zip', 'C:\TEMP\with space\changelog'

& 7z.exe $prm

BTW, Invoke-Expression with one parameter works for me, too, e.g. this works

$cmd = '& 7z.exe a -tzip "c:\temp\with space\test2.zip" "C:\TEMP\with space\changelog"'

Invoke-Expression $cmd

P.S. I usually prefer the way with a parameter array because it is easier to compose programmatically than to build an expression for Invoke-Expression.

Zombo
  • 1
  • 62
  • 391
  • 407
Roman Kuzmin
  • 40,627
  • 11
  • 95
  • 117
66

Try invoking your command with Invoke-Expression:

Invoke-Expression $cmd1

Here is a working example on my machine:

$cmd = "& 'C:\Program Files\7-zip\7z.exe' a -tzip c:\temp\test.zip c:\temp\test.txt"
Invoke-Expression $cmd

iex is an alias for Invoke-Expression so you could do:

iex $cmd1

For a full list : Visit https://ss64.com/ps/ for more Powershell stuff.

Good Luck...

Aakash
  • 21,375
  • 7
  • 100
  • 81
kbrimington
  • 25,142
  • 5
  • 62
  • 74
  • Tells me The term '7z.exe a -tzip c:\arc_logs\site-host-at-web1-100827.zip c:\inetpub\logs\logfiles\w3svc1\u_ex100827.log' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At :line:14 char:1 + & <<<< $cmd1 – Travis Aug 28 '10 at 23:29
  • @Travis: Oops. the ampersand is works if there are no args. I updated the post with a solution for your command. – kbrimington Aug 28 '10 at 23:32
  • I have tried invoke-expression before and it has not worked. It spits out the error: Bad numeric constant: 7. At :line:1 char:2 + 7z <<<< .exe a -tzip c:\arc_logs\site-host-at-web1-100827.zip c:\inetpub\logs\logfiles\w3svc1\u_ex100827.log It almost seems like it is trying to evaluate it instead of executing it. – Travis Aug 28 '10 at 23:47
  • @Travis: It may be that 7z.exe is not on your path. Verify that it is on the path and/or try giving the full path name to the executable in your expression. – kbrimington Aug 28 '10 at 23:59
  • 1
    @Travis: I just confirmed on my own system that 7z.exe gave the error you described, just typing from the command prompt, but with the full path to 7z.exe (for me, it was `'C:\Program Files\7-zip\7z.exe'`, I could execute 7z.exe. – kbrimington Aug 29 '10 at 00:04
  • Yes i test that out and added 7z.exe to the path. Still have the problem. – Travis Aug 29 '10 at 00:48
  • @Travis: Sorry. It takes cobbling together all the elements of our conversation. I've updated my post with a working example. You will need to change out the paths, but the structure should still apply. – kbrimington Aug 29 '10 at 01:05