Installing or updating 7-Zip using PowerShell

UPDATE 2019-12-12: Included a fix for possible issues with SSL connections and the script getting the wrong url. Tested with newest version at the time of updating.

With the new version 18.05 released. I was met with a challenge to update allot of clients and my own computer in a way because of a security breach that was patched. So I took what I learned from other scripts I created and created this new script to install or update 7-Zip using PowerShell. Now of course the argument against this could be made for why create this because stable versions of 7-Zip do not come often. However it is a script that I share in the case that you need it. Maybe that is not now but when you do need to quickly update your own or other computers with the latest 7-Zip build here you are.

Some things to note is that the script currently uses C:\Users\[username]\AppData\7Zip as the temporary work directory to download the exe and install it from. In an example in the post I will show you how to change that.

So let’s get straight into how it works and start with the basics.

./Update-7Zip.ps1

This example is the easiest one of course it uses the default options of the script. In this case the script will automatically detect your systems architecture and after that it will of course download and install the latest version of 7-Zip using the official website.

./Update-7Zip.ps1 -Verbose

The script was created to just do its job and nothing more. So there is no output to the screen by default. However you can use the example above to get debugging output of the script to see how it performs and works or hangs.

As you can see from the picture it automatically detects the architecture of the system I am running this script on. It detects in my case that the system is 64bit and of course downloads and installs that version.

You could of course change this. The architecture parameter by default is set to Detect. However if you would rather want to use a specific version you can use the Architecture switch and use either 32 or 64 as parameter options to download and install the version of 7-Zip you want. For example under here I am saying to the script that I would like to use the 32 bit version of 7-Zip instead of it otherwise automatically detecting 64 bit.

./Update-7Zip.ps1 -Architecture 32

If for any reason you would rather want to use another downloading directory to be used instead of C:\Users\[username]\AppData\Temp\7Zip then I included the parameter TemporaryDownloadFolder. It’s not the best name but it gives you the option to use another folder created or not created. The script will detect if the folder does not exist and attempt to create it.

./Update-7Zip.ps1 -TemporaryDownloadFolder "C:\Temp\"

The last example is used for if you would rather see the installing GUI appear. By default the script will silently install 7-Zip however if you would like to see the GUI and take action upon it you can use the Show switch parameter to allow you to do so.

./Update-7Zip.ps1 -Show

Keep in mind that the script is written so you can mix and match parameters and switches that are supported in the script. Also note that I created this out of speed for my deployment purposes and I am still learning PowerShell. Feedback is of course always welcome.

If you are interested you can use the script under here.

<#
.SYNOPSIS
This is a simple Powershell script to install and update 7-Zip from official sources silently

.DESCRIPTION
The script will download based on the detected architecture of the host and the channel selected the newest version it can find from the 7-Zip official static releases. 

.EXAMPLE
Update-7Zip.ps1
Update 7-Zip using the default parameters in which it will download the latest released version based on automatically detecting your host systems architecture

.EXAMPLE
Update-7Zip.ps1 -Architecture 32
Update or install 7-Zip using the stable release channel and instead of auto detection of the architecture it will download the 32 bit version

.EXAMPLE
Update-7Zip.ps1 -Architecture 64
Update or install 7-Zip using the stable release channel and instead of auto detection of the architecture it will download the 64 bit version

.EXAMPLE
Update-7Zip.ps1 -Show
Using the Show parameter it will show you the Setup screen for 7-Zip instead of installing silently

.LINK
https://www.7-zip.org/
https://perplexity.nl/
#>

[CmdletBinding()]
param
(
    [Parameter()][ValidateSet('64', '32', 'Detect')][string]$Architecture = "Detect",
    [Parameter()][string]$TemporaryDownloadFolder = "C:\Users\" + $($env:username) + "\AppData\Local\TempZip\",
    [Parameter()][Switch]$Show
)

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

#Declares
$7zipWebsite = 'https://7-zip.org/'
$TemporaryDownloadFile = $TemporaryDownloadFolder + "7zip.exe" 

#Detect Architecture
if ($Architecture -eq 'Detect')
{
    Write-Verbose "Detecting host operating system architecture to download according version of 7-Zip"
    if ([Environment]::Is64BitOperatingSystem)
    {
        $Architecture = '64'
    }
    else 
    {
        $Architecture = '32'
    }
}

#Check if a temp folder exist if so recreate it
Write-Verbose "Creating temporary work directory in $TemporaryDownloadFolder"
if (-Not(Test-Path $TemporaryDownloadFolder))
{
    New-Item -Path $TemporaryDownloadFolder -ItemType Directory | Out-Null
}

#Check if a temp download exists if so delete it and redownload it
Write-Verbose "Checking if a download already exists if so remove it"
if(Test-Path $TemporaryDownloadFile)
{
    Remove-Item $TemporaryDownloadFile
}

#Based on the Architecture selected or detected request what the newest version of 7-zip is
Write-Verbose "Requested the download url of the newest 7-Zip version available based on selected architecture $Architecture bit"
if ($Architecture -eq 64)
{
    $webLocation = $7zipWebsite + (Invoke-WebRequest -Uri $7zipWebsite | Select-Object -ExpandProperty Links | Where-Object {($_.innerHTML -eq 'Download') -and ($_.href -like "a/*") -and ($_.href -like "*-x64.exe")} | Select-Object -ExpandProperty href).Split(' ')[0]
}
else
{
    $webLocation = $7zipWebsite + (Invoke-WebRequest -Uri $7zipWebsite | Select-Object -ExpandProperty Links | Where-Object {($_.innerHTML -eq 'Download') -and ($_.href -like "a/*") -and ($_.href -notlike "*-x64.exe")} | Select-Object -ExpandProperty href).Split(' ')[0]
}

#Downloading based on the 
Write-Verbose "Start the download of $weblocation to $TemporaryDownloadFile"
Invoke-WebRequest $webLocation -OutFile $TemporaryDownloadFile


Write-Verbose "Installing the downloaded 7-Zip version"
if ($Show)
{
    Start-Process $TemporaryDownloadFile -Wait
}
else 
{ 
    Start-Process $TemporaryDownloadFile -ArgumentList "/S" -Wait
}

Write-Verbose "Cleaning up the temporary work directory that was used"
Remove-Item $TemporaryDownloadFolder -Recurse -Force

Write-Verbose "Script has been completed"

Leave a Comment