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:

  1. Open PowerShell ISE as an administrator.
  2. 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 ...

Get SQL Server 2014 with PowerShell v5 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.