Reputation: 75
I want my script be running on both Win 7 and Win 10 system to generate either zip or 7z files.
On Win10, the Add-Type
way can be used to generate zip file.
Add-Type -assembly "system.io.compression.filesystem"
[io.compression.zipfile]::CreateFromDirectory($Source, $destination)
However, on Win7 it doesn't work. So I just change the script as:
cmd /c "C:\Program Files\7-Zip\7z.exe" a "D:\$folderName.7z" $source
It works however, it's not guaranteed that 7z.exe is installed under C:\Program Files\7-Zip
for every user.
So I was wondering how can I write a powershell script usable on both Win7 and Win10 without specifying zip.exe
path.
Upvotes: 0
Views: 65
Reputation: 2639
If upgrading to PowerShell 5 is an option, then it's easy: just use the Compress-Archive cmdlet.
Upvotes: 2
Reputation: 24585
Here's one way to check if 7-Zip is installed:
$sevenZip = Join-Path ([Environment]::GetFolderPath([Environment+SpecialFolder]::ProgramFiles)) "7-Zip\7z.exe"
if ( -not (Test-Path -LiteralPath $sevenZip) ) {
Write-Error "7-Zip is required." -Category ObjectNotFound
return # or exit, if you are in a script
}
# Call it this way:
# & $sevenZip arg1 [arg2 [...]]
Upvotes: 1