Skip to content

Instantly share code, notes, and snippets.

@nitsh
Created January 21, 2019 11:31
Show Gist options
  • Save nitsh/48e4bdc19e5b94252fb2fc59c0bbc1fe to your computer and use it in GitHub Desktop.
Save nitsh/48e4bdc19e5b94252fb2fc59c0bbc1fe to your computer and use it in GitHub Desktop.
Install 7Zip in windows from powershell
#Inspired from https://github.com/ferventcoder/vagrant-windows-puppet/blob/master/boxes/shared/shell/InstallPuppetFromMSI.ps1
$7ZipUrl = "https://www.7-zip.org/a/7z1806.msi"
$7ZipInstallerPath = 'c:\vagrant\bin'
$7ZipInstallerFile = '7z1806.msi'
$7ZipInstaller = Join-Path $7ZipInstallerPath $7ZipInstallerFile
if ([System.IntPtr]::Size -eq 8) {
Write-Host "Going to 7Zip 64-bit."
$7ZipUrl = "https://www.7-zip.org/a/7z1806-x64.msi"
$7ZipInstallerFile = '7z1806-x64.msi'
}
$7ZipInstalled = $false
try {
$ErrorActionPreference = "Stop";
Get-Command 7z | Out-Null
$7ZipInstalled = $true
$7ZipVersion=&7z "--version"
Write-Host "7Zip $7ZipVersion is installed. This process does not ensure the exact version or at least version specified, but only that 7Zip is installed. Exiting..."
# Exit 0
} catch {
Write-Host "7Zip is not installed, continuing..."
}
if (!($7ZipInstalled)) {
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
if (! ($currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))) {
Write-Host -ForegroundColor Red "You must run this script as an administrator."
Exit 1
}
if (!(Test-Path $7ZipInstallerPath)) {
Write-Host "Creating folder `'$7ZipInstallerPath`'"
$null = New-Item -Path "$7ZipInstallerPath" -ItemType Directory
}
if (!(Test-Path $7ZipInstaller)) {
Write-Host "Downloading `'$7ZipUrl`' to `'$7ZipInstaller`'"
(New-Object Net.WebClient).DownloadFile("$7ZipUrl","$7ZipInstaller")
}
# Install it - msiexec will download from the url
$install_args = @("/qn", "/norestart","/i", "$7ZipInstaller")
Write-Host "Installing 7Zip. Running msiexec.exe $install_args"
$process = Start-Process -FilePath msiexec.exe -ArgumentList $install_args -Wait -PassThru
if ($process.ExitCode -ne 0) {
Write-Host "Installer failed."
Exit 1
}
Write-Host "7Zip successfully installed."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment