I'm trying to run a Powershell command to call 7-Zip to zip up a folder using the following command:
$command = $SevenZip + " a " + $targetDirForZip + $GetDateName + "_" + $dir.Name + ".7z " + $dir.FullName
Invoke-Expression $command
The variables being fed into $command are already set and $SevenZip is "c:\Program Files\7-Zip\7z.exe"
This isn't working and I'm trying to work out the best way to call 7-Zip from Powershell. Ideas?
The Invoke-Expression cmdlet evaluates or runs a specified string as a command and returns the results of the expression or command. Without Invoke-Expression , a string submitted at the command line is returned (echoed) unchanged. Expressions are evaluated and run in the current scope.
One of the PowerShell features is the use of compressed or abbreviated cmdlet names. Instead of using the full name, 'Invoke-Expression' is most of the time replaced by 'IEX'. This three-characters string is then replaced by something more unreadable. Example 1: Some characters are replaced: 'DEX'.replace('D','I')
Description. The Invoke-Command cmdlet runs commands on a local or remote computer and returns all output from the commands, including errors. Using a single Invoke-Command command, you can run commands on multiple computers. To run a single command on a remote computer, use the ComputerName parameter.
I've had the same problem before. This is code (almost) straight from a backup script that I use currently:
[string]$pathToZipExe = "C:\Program Files\7-zip\7z.exe";
[Array]$arguments = "a", "-tgzip", $outputFilePath, $inputFilePath;
& $pathToZipExe $arguments;
I've gotten into the habit of using an argument array with the call operator, It seems to be more robust than other methods.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With