Skip to content

Instantly share code, notes, and snippets.

@alst74
Created February 1, 2017 09:34
Show Gist options
  • Save alst74/499a101f89d92d99c48cdd6f3e507254 to your computer and use it in GitHub Desktop.
Save alst74/499a101f89d92d99c48cdd6f3e507254 to your computer and use it in GitHub Desktop.
Powershell v3 unzip
function Expand-ZIPFile($file, $destination) {
$shell = new-object -com shell.application
$zip = $shell.NameSpace($file)
foreach($item in $zip.items())
{
$shell.Namespace($destination).copyhere($item)
}
}
Expand-ZIPFile –File testfile.zip –Destination c:\temp
@alst74
Copy link
Author

alst74 commented Jun 21, 2017

Another one I haven't tested:

$sourceFile = 'C:\assets\Microsoft.Azure.ServiceFabric.WindowsServer.5.3.204.9494.zip'
$targetFolder = 'C:\Microsoft.Azure.ServiceFabric.WindowsServer'
 
[System.Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem')
[System.IO.Compression.ZipFile]::ExtractToDirectory($sourceFile, $targetFolder)

@alst74
Copy link
Author

alst74 commented Jun 21, 2017

Powershell v5 -

Get-Command *archive | ft CommandTYpe, Name -a

CommandType Name
———– —-
Function Compress-Archive
Function Expand-Archive

To compress

$files = Get-ChildItem -Path C:\Scripts -Filter *.csv | select -ExpandProperty Fullname
Compress-Archive -Path $files -DestinationPath C:\Scripts\t1.zip -CompressionLevel Optimal

or a single file

Compress-Archive -Path c:\scripts\test.csv -DestinationPath C:\Scripts\t2.zip -CompressionLevel Optimal

To uncompress

Expand-Archive -Path C:\Scripts\t1.zip -DestinationPath c:\scripts

if you need to overwrite files:

Expand-Archive -Path C:\Scripts\t1.zip -DestinationPath c:\scripts -Force

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment