Using PowerShell to Create ZIP Archives
to a Different Directory

Blogs / Using PowerShell to Create ZIP Archives to a Different Directory

Jesse Torres June 13, 2022 4:20pm

In this blog post I will show you how to move files, delete files, and compress files using Power Shell. We will be using the command lines like: Move-Item, Get-Childitem, and Compress-Archive.

In this example, we are going to be moving txt files with a specific file extension to a different directory and then create a single zip file from them. At the end of this guide you will be able to move files to a different directory, delete specific files from a directory, and compress files into a zip file.

#Move files in directory and compress them
$date = Get-Date -Format "MMddyyHHmm"

Get-ChildItem -Path "C:\Users\jtorres1\Downloads\PowerShell\Upload Files\*" -Include *.2,*3 | Move-Item -Destination "C:\Users\jtorres1\Downloads\PowerShell\archived files"

$compress = @{
  Path = "C:\Users\jtorres1\Downloads\PowerShell\archived files\*.2", "C:\Users\jtorres1\Downloads\PowerShell\archived files\*.3"
  CompressionLevel = "Fastest"
  DestinationPath = "C:\Users\jtorres1\Downloads\PowerShell\archived files\$date.zip"
}

Compress-Archive @compress | Out-Null
Get-ChildItem "C:\Users\jtorres1\Downloads\PowerShell\archived files\*.2", "C:\Users\jtorres1\Downloads\PowerShell\archived files\*.3" | Remove-Item
exit
              

Want More Free Content?

If you want more free technical guides, click below and subscribe to my You Tube channel.