Compress-Archive ` -LiteralPath "C:\temp\NewFolder\New Text Document.txt", "C:\temp\apps" ` -CompressionLevel Optimal ` -DestinationPath "C:\raja\Draft.Zip"The above script creates a zip file, It takes files from C:\temp\NewFolder\New Text Document.txt and C:\temp\apps and creates a Draft.zip in C:\raja\Draft.Zip, see the screenshot below. Example 2
Compress-Archive ` -Path C:\temp\apps ` -CompressionLevel Fastest ` -DestinationPath C:\raja\simple.zipLet’s create a function and make it useful whenever we need it.
function Zip-Files { param ( [string]$ZipFileName, [string]$SourceDirectory ) Add-Type -Assembly System.IO.Compression.FileSystem $Compress = [System.IO.Compression.CompressionLevel]::Optimal [System.IO.Compression.ZipFile]::CreateFromDirectory($SourceDirectory, $ZipFileName, $Compress, $false) }The variable used in this script is. $ZipFileName = You need to specify the path where you want to create zip file and a name $SourceDirectory = You need to specify the path which file or folder you want to zip.
Zip-Files -SourceDirectory C:\temp\apps -ZipFileName C:\raja\test.zip
Thank you for reading this article, if you have any questions please let us know. Thank you for visiting my site, for any scripts in these articles you are testing please make sure you have tested this script in our lower environment before you run in production.
Leave a Reply Cancel reply