Zip and Unzip File in Powershell 4

后端 未结 5 1692
一生所求
一生所求 2021-02-13 22:14

I am using Windows Server 2012 R2 (64 bit). I have powershell version 4 available in it. I am trying to zip and unzip files. When I try Write-Zip command, it throws me following

5条回答
  •  Happy的楠姐
    2021-02-13 22:41

    You can use custom powershell object New-Object -ComObject Shell.Application and copy the file with flags to unzip.

    $filePath = "foo.zip"
    $shell = New-Object -ComObject Shell.Application
    $zipFile = $shell.NameSpace($filePath)
    $destinationFolder = $shell.NameSpace("C:\Program Files\WindowsPowerShell\Modules")
    
    $copyFlags = 0x00
    $copyFlags += 0x04 # Hide progress dialogs
    $copyFlags += 0x10 # Overwrite existing files
    
    $destinationFolder.CopyHere($zipFile.Items(), $copyFlags)
    

    Credit source https://github.com/hashicorp/best-practices/blob/master/packer/scripts/windows/install_windows_updates.ps1#L12-L22

    This does not work with windows 'core' edition. If possible, upgrade to powershell 5 and use Expand Archive since it is much simpler.

提交回复
热议问题