Using Invoke-Expression
In this recipe, we will use the Invoke-Expression cmdlet.
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. Go to Start | Accessories | Windows PowerShell | Windows PowerShell ISE.
Add the following script and run it:
$VerbosePreference = "Continue" $program = "`"C:\Program Files\7-Zip\7z.exe`"" $7zargs = " a -tzip " $zipFile = " `"C:\Temp\new archive.zip`" " $directoryToZip = " `"C:\Temp\old`" " $cmd = "& $program $7zargs $zipFile $directoryToZip " #display final command Write-Verbose $cmd Invoke-Expression $cmd $VerbosePreference = "SilentlyContinue"
How it works...
The Invoke-Expression
cmdlet allows PowerShell expressions to be run from PowerShell. These expressions can consist of other PowerShell statements and functions, or they can contain executables and arguments.
In this recipe, we are composing...