PoshCode Logo PowerShell Code Repository

Unzip Files by mr64bit 2 weeks ago (modification of post by JayneticMuffin view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/6839"></script>download | new post

Wanted to create an unzip function for single files with an option to delete the originating zip, and also have a function for scanning through a folder structure recursively and providing the same functionality… and here it is.

  1. # Unzip the file and keep the zip
  2. # Unzip-File -FileName 'test.zip'
  3. # Unzip the file and delete the zip
  4. # Unzip-File -FileName 'test.zip' -DeleteSource $True
  5. function Unzip-File {
  6.         param (
  7.                 [parameter(mandatory=$true][ValidateNotNullOrEmpty()]$fileName,
  8.                 $DeleteSource = $false
  9.         )
  10.         $fileInfo = Get-Item -Path $FileName
  11.         $appName = New-Object -ComObject Shell.Application
  12.         $zipName = $appName.NameSpace($fileInfo.FullName)
  13.         $extPath = $fileInfo.Directory.FullName + '\' + $fileInfo.BaseName
  14.         $null = New-Item -Path $extPath -ItemType Directory -Force
  15.         $dstFolder = $appName.NameSpace($extPath)
  16.         $dstFolder.Copyhere($zipName.Items())
  17.         If ($DeleteSource) {Remove-Item -Path $fileInfo.FullName}
  18. }
  19. function Unzip-MultipleFiles {
  20.         param (
  21.                 [parameter(mandatory=$true)][ValidateNotNullOrEmpty()][string]$Path,
  22.                 $DeleteSource = $false
  23.         )
  24.         $Files = Get-ChildItem -Path $Path -Recurse -Include '*.zip' | Select FullName,Directory,BaseName
  25.         $Files | % {
  26.                 Unzip-File -FileName $_.FullName
  27.                 If ($DeleteSource) {Remove-Item -Path $_.FullName}
  28.         }
  29. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me