PowerShell Unzip File

PowerShell Unzip File

OK, I seem to run into this every now and then, so I decided to write it down so I don't forget it.
If you need to extract a zip file in PowerShell use the following:

Expand-Archive -Path "nugetPackage.zip" "z:\Temp\nugetPackage"

You can make this a function, but that seems a little redundant, since it's already a function:

function UnzipFile
{
    param([string] $source, [string] $destination)
    Expand-Archive -Path "$source" "$destination"
}

And you can call the new function like this:

UnzipFile "nugetPackage.zip" "z:\Temp\nugetPackage"

You might get unsupported archive file type, so you can always rename the file to .zip to get around this; assuming the file is in zip format, but has a different file extension. This happens with .xap, .jar, .nupkg, etc...

mv nugetPackage.nupkg nugetPackage.zip
Expand-Archive -Path "nugetPackage.zip" "z:\Temp\nugetPackage"

Error you will see if the file isn't a zip file
Expand-Archive : .nupkg is not a supported archive file format. .zip is the only supported archive file format.
At line:4 char:5
+     Expand-Archive -Path "$source" "$destination"
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (.nupkg:String) [Expand-Archive], IOException

    + FullyQualifiedErrorId : NotSupportedArchiveFileExtension,Expand-Archive

Comments

Popular posts from this blog

Azure DevOps - Auto Approve PR with PAT

Ubuntu 18.04 - Install Android Emulator

Ansible Module - VMWare Update Guest PCI Device