powershellpdfzipcompressionalteryx

How to use Powershell Compress-Archive to compress only pdfs in a folder?


I'm working on an Alteryx workflow which creates multiple pdfs and outputs them to a directory. I need to call a Powershell script at the end of the workflow to compress the pdfs into a zip file and save it in the same location.

I found this: PowerShell Compress-Archive By File Extension, but it requires copying the files to another location and then compressing them. Is there a way I can just compress all pdfs in a certain folder location and output the zip file in the same location? Due to restrictions in Alteryx I might not be able to work with the output if it is in a different location.

My current Powershell script:

Compress-Archive -LiteralPath 'D:\temp\test zipping flow\' -DestinationPath 'D:\temp\final.zip' -Force

This works perfectly, but tries to zip files with other extensions as well but I only want .pdf files.

Any suggestions are greatly appreciated.


Solution

  • Instead of -LiteralPath use -Path, so you can add a wildcard character *

    Compress-Archive -Path 'D:\temp\test zipping flow\*.pdf' -DestinationPath 'D:\temp\final.zip' -Force
    

    As alternative for when you need to use -LiteralPath (perhaps because the path contains characters that would be interpreted using -Path like square brackets), you could do this instead:

    Get-ChildItem -LiteralPath 'D:\temp\test zipping flow' -Filter '*.pdf' -File | 
    Compress-Archive -DestinationPath 'D:\temp\final.zip' -Force