Using Invoke-Expression
In this recipe, we will use the Invoke-Expression
cmdlet to compress some files using a free compression utility.
Getting ready
For this recipe, we will use the 7-Zip application to compress some files. Download 7-Zip from http://www.7-zip.org/.
How to do it...
Let's check out the Invoke-Expression
cmdlet:
Open PowerShell ISE as an administrator.
Add the following script and run it:
$VerbosePreference = "Continue" $program = "`"C:\Program Files\7-Zip\7z.exe`"" #arguments $7zargs = " a -tzip " $zipFile = " `"C:\Temp\new archive.zip`" " $directoryToZip = " `"C:\Temp\old`" " #compose the command $cmd = "& $program $7zargs $zipFile $directoryToZip " #display final command Write-Verbose $cmd #execute the command Invoke-Expression $cmd $VerbosePreference = "SilentlyContinue"
How it works...
The Invoke-Expression
cmdlet allows PowerShell expressions to run from PowerShell. These expressions can consist of other PowerShell statements and functions, or they can contain executables...