Skip to content

Instantly share code, notes, and snippets.

@sweeneyrobb
Created October 29, 2014 17:07
Show Gist options
  • Select an option

  • Save sweeneyrobb/bdeef74630a38086e3b3 to your computer and use it in GitHub Desktop.

Select an option

Save sweeneyrobb/bdeef74630a38086e3b3 to your computer and use it in GitHub Desktop.
Creates a zip file and adds a file or directory
function New-Zip ( $zipFile, $file ) {
if ($file -is [string]) {
$file = Get-Item $file
}
set-content $zipFile ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
$zipFile = Get-Item $zipFile
$zipFile.IsReadOnly = $false
$zip = (New-Object -ComObject shell.application).NameSpace($zipFile.FullName)
if ($file.PSIsContainer) {
$file | get-childitem -exclude $zipFile.Name | foreach-object {
if ($_.Name -ne $zipFile.Name) {
$zip.CopyHere($_.FullName)
# not exactly sure why the wait is needed. prompts dialogs if not here.
Start-Sleep -Milliseconds 200
}
}
} else { $zip.CopyHere($file.FullName) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment