Powershell compress-archive exclude files

In PowerShell, the “Compress-Archive” cmdlet is used to compress one or more files into a zip archive. It allows excluding specific files or directories from the compression process using the “-ExcludePath” parameter. Here’s an example:


$sourcePath = "C:\Path\To\Files"
$destinationPath = "C:\Path\To\Archive.zip"
$excludePath = "C:\Path\To\Files\Exclude"

Compress-Archive -Path $sourcePath -DestinationPath $destinationPath -ExcludePath $excludePath
  

In the above example, the contents of the “C:\Path\To\Files” directory will be compressed into a zip file located at “C:\Path\To\Archive.zip”. The “C:\Path\To\Files\Exclude” directory and its contents will be excluded from the compression process.

Multiple files or directories can be excluded by providing an array of paths to the “-ExcludePath” parameter. Here’s an example:


$sourcePath = "C:\Path\To\Files"
$destinationPath = "C:\Path\To\Archive.zip"
$excludePaths = @("C:\Path\To\Files\Exclude1", "C:\Path\To\Files\Exclude2")

Compress-Archive -Path $sourcePath -DestinationPath $destinationPath -ExcludePath $excludePaths
  

In this example, both “C:\Path\To\Files\Exclude1” and “C:\Path\To\Files\Exclude2” directories (and their contents) will be excluded from the compression process.

Leave a comment