• Home
  • Blog
  • Contact
  • About Us
     
No Result
View All Result
  • Home
  • Blog
  • Contact
  • About Us
No Result
View All Result
Tutorial
No Result
View All Result

How To Install 7-Zip Using PowerShell

Table of Contents

Purpose of the Script

  • Automatically download and install the latest version of 7-Zip (x64 EXE)
  • Uses Microsoft’s Winget GitHub repository to locate the official installer.
  • Performs a silent installation for all users.
# Define the GitHub API URL for the app manifests in winget-pkgs.
$apiUrl = "https://api.github.com/repos/microsoft/winget-pkgs/contents/manifests/7/7zip/7zip"

# Fetch version folders then filter only version folders.
$versions = Invoke-RestMethod -Uri $apiUrl -Headers @{ 'User-Agent' = 'PowerShell' }
$versionFolders = $versions | Where-Object { $_.type -eq "dir" }

# Extract and sort version numbers to get the latest version.
$sortedVersions = $versionFolders | ForEach-Object { $_.name } | Sort-Object {[version]$_} -Descending -ErrorAction SilentlyContinue
$latestVersion = $sortedVersions[0]

Write-Host "Latest 7-Zip version: $latestVersion"

# Get contents of the latest version folder to find the .installer.yaml file.
$latestApiUrl = "$apiUrl/$latestVersion"
$latestFiles = Invoke-RestMethod -Uri $latestApiUrl -Headers @{ 'User-Agent' = 'PowerShell' }
$installerFile = $latestFiles | Where-Object { $_.name -like "*.installer.yaml" }

# Download and parse YAML content to get the Url of the latest installer file.
$yamlUrl = $installerFile.download_url
$yamlContent = Invoke-RestMethod -Uri $yamlUrl -Headers @{ 'User-Agent' = 'PowerShell' }
$yamlString = $yamlContent -join "`n"
$installerUrls = [regex]::Matches($yamlString, "InstallerUrl:\s+(http[^\s]+)") | ForEach-Object { $_.Groups[1].Value }
$installerUrl = $installerUrls[1]

Write-Host "Downloading installer from: $installerUrl"

# Download the latest installer to the temp folder.
$webClient = [System.Net.WebClient]::new()
$webClient.DownloadFile($installerUrl, "$env:TEMP\7zip-latest.exe")

# Start the install process.
Start-Process -FilePath "$env:TEMP\7zip-latest.exe" -ArgumentList '/S' -Wait

# Delete the downloaded installer file.
Remove-Item -Path "$env:TEMP\7zip-latest.exe" -Force -ErrorAction SilentlyContinue

Write-Host "7-Zip installation completed."

Step-by-Step Explanation

Below is a detailed explanation of what each part of the PowerShell script does. The script is designed to automatically install or update 7-Zip on Windows computers.

1. Define the API URL

$apiUrl = "https://api.github.com/repos/microsoft/winget-pkgs/contents/manifests/7/7zip/7zip"
  • Points to the 7-Zip manifest folder in the winget-pkgs GitHub repository.
  • This folder contains subfolders for each version (e.g., 23.01, 24.00, etc.).

2. Get the list of available versions

$versions = Invoke-RestMethod -Uri $apiUrl -Headers @{ 'User-Agent' = 'PowerShell' }
$versionFolders = $versions | Where-Object { $_.type -eq "dir" }
  • Fetches all items in the folder (both files and folders).
  • Filters to only include directories, because each directory represents a 7-Zip version.
# Output
name         : 16.04
path         : manifests/7/7zip/7zip/16.04
sha          : dea6139429eac7e3bfeddb30963c61869d6d8a86
size         : 0
url          : https://api.github.com/repos/microsoft/winget-pkgs/contents/manifests/7/7zip/7zip/16.04?ref=master
html_url     : https://github.com/microsoft/winget-pkgs/tree/master/manifests/7/7zip/7zip/16.04
git_url      : https://api.github.com/repos/microsoft/winget-pkgs/git/trees/dea6139429eac7e3bfeddb30963c61869d6d8a86
download_url : 
type         : dir
_links       : @{self=https://api.github.com/repos/microsoft/winget-pkgs/contents/manifests/7/7zip/7zip/16.04?ref=maste
               r; git=https://api.github.com/repos/microsoft/winget-pkgs/git/trees/dea6139429eac7e3bfeddb30963c61869d6d
               8a86; html=https://github.com/microsoft/winget-pkgs/tree/master/manifests/7/7zip/7zip/16.04}
...

3. Extract and sort versions

$sortedVersions = $versionFolders | ForEach-Object { $_.name } | Sort-Object {[version]$_} -Descending -ErrorAction SilentlyContinue
$latestVersion = $sortedVersions[0]
  • Extracts folder names (version numbers).
  • Sorts them as version objects (not strings) in descending order.
  • Picks the latest version (first in the sorted list).
# Output
PS C:\> $sortedVersions
25.00
24.09
24.08
24.07
24.06
24.05
...
Alpha
PS C:\> $latestVersion
25.00

4. Get the .installer.yaml file for the latest version

$latestApiUrl = "$apiUrl/$latestVersion"
$latestFiles = Invoke-RestMethod -Uri $latestApiUrl -Headers @{ 'User-Agent' = 'PowerShell' }
$installerFile = $latestFiles | Where-Object { $_.name -like "*.installer.yaml" }
  • Looks inside the latest version folder.
  • Finds the *.installer.yaml file, which contains metadata about the installer (URLs, architecture, etc.).
# Output
name         : 7zip.7zip.installer.yaml
path         : manifests/7/7zip/7zip/25.00/7zip.7zip.installer.yaml
sha          : 5e4a168141b6de6a40aad780b6bead2b05afb230
size         : 3009
url          : https://api.github.com/repos/microsoft/winget-pkgs/contents/manifests/7/7zip/7zip/25.00/7zip.7zip.instal
               ler.yaml?ref=master
html_url     : https://github.com/microsoft/winget-pkgs/blob/master/manifests/7/7zip/7zip/25.00/7zip.7zip.installer.yam
               l
git_url      : https://api.github.com/repos/microsoft/winget-pkgs/git/blobs/5e4a168141b6de6a40aad780b6bead2b05afb230
download_url : https://raw.githubusercontent.com/microsoft/winget-pkgs/master/manifests/7/7zip/7zip/25.00/7zip.7zip.ins
               taller.yaml
type         : file
_links       : @{self=https://api.github.com/repos/microsoft/winget-pkgs/contents/manifests/7/7zip/7zip/25.00/7zip.7zip
               .installer.yaml?ref=master; git=https://api.github.com/repos/microsoft/winget-pkgs/git/blobs/5e4a168141b
               6de6a40aad780b6bead2b05afb230; html=https://github.com/microsoft/winget-pkgs/blob/master/manifests/7/7zi
               p/7zip/25.00/7zip.7zip.installer.yaml}

5. Extract installer URLs from YAML to find the URL of the latest version

$yamlUrl = $installerFile.download_url
$yamlContent = Invoke-RestMethod -Uri $yamlUrl -Headers @{ 'User-Agent' = 'PowerShell' }
$yamlString = $yamlContent -join "`n"
$installerUrls = [regex]::Matches($yamlString, "InstallerUrl:\s+(http[^\s]+)") | ForEach-Object { $_.Groups[1].Value }
$installerUrl = $installerUrls[1]
  • Downloads the YAML file as raw text.
  • Extracts all InstallerUrl entries using regular expressions.
  • Picks the URL using an index number.
# Output
PS C:\> $installerUrls
https://7-zip.org/a/7z2500.exe
https://7-zip.org/a/7z2500-x64.exe
https://7-zip.org/a/7z2500-arm.exe
https://7-zip.org/a/7z2500-arm64.exe
https://7-zip.org/a/7z2500.msi
https://7-zip.org/a/7z2500-x64.msi

PS C:\> $installerUrl
https://7-zip.org/a/7z2500-x64.exe

6. Download the installer

$webClient = [System.Net.WebClient]::new()
$webClient.DownloadFile($installerUrl, "$env:TEMP\7zip-latest.exe")
  • Downloads the installer to the Windows Temp folder.

7. Install 7-Zip silently

Start-Process -FilePath "$env:TEMP\7zip-latest.exe" -ArgumentList '/S' -Wait
  • Runs the installer in silent mode, no user interaction.

8. Clean up the installer and notify to user

Remove-Item -Path "$env:TEMP\7zip-latest.exe" -Force -ErrorAction SilentlyContinue
Write-Host "7-Zip installation completed."
  • Deletes the installer after installation to keep the system clean.
  • Outputs a message to confirm the installation status.

How to install using PowerShell

To install the app, simply open PowerShell as an administrator, copy the code snippets below, paste them into the PowerShell window, and press .

# Define the GitHub API URL for the app manifests in winget-pkgs.
$apiUrl = "https://api.github.com/repos/microsoft/winget-pkgs/contents/manifests/7/7zip/7zip"

# Fetch version folders then filter only version folders.
$versions = Invoke-RestMethod -Uri $apiUrl -Headers @{ 'User-Agent' = 'PowerShell' }
$versionFolders = $versions | Where-Object { $_.type -eq "dir" }

# Extract and sort version numbers to get the latest version.
$sortedVersions = $versionFolders | ForEach-Object { $_.name } | Sort-Object {[version]$_} -Descending -ErrorAction SilentlyContinue
$latestVersion = $sortedVersions[0]

Write-Host "Latest 7-Zip version: $latestVersion"

# Get contents of the latest version folder to find the .installer.yaml file.
$latestApiUrl = "$apiUrl/$latestVersion"
$latestFiles = Invoke-RestMethod -Uri $latestApiUrl -Headers @{ 'User-Agent' = 'PowerShell' }
$installerFile = $latestFiles | Where-Object { $_.name -like "*.installer.yaml" }

# Download and parse YAML content to get the Url of the latest installer file.
$yamlUrl = $installerFile.download_url
$yamlContent = Invoke-RestMethod -Uri $yamlUrl -Headers @{ 'User-Agent' = 'PowerShell' }
$yamlString = $yamlContent -join "`n"
$installerUrls = [regex]::Matches($yamlString, "InstallerUrl:\s+(http[^\s]+)") | ForEach-Object { $_.Groups[1].Value }
$installerUrl = $installerUrls[1]

Write-Host "Downloading installer from: $installerUrl"

# Download the latest installer to the temp folder.
$webClient = [System.Net.WebClient]::new()
$webClient.DownloadFile($installerUrl, "$env:TEMP\7zip-latest.exe")

# Start the install process.
Start-Process -FilePath "$env:TEMP\7zip-latest.exe" -ArgumentList '/S' -Wait

# Delete the downloaded installer file.
Remove-Item -Path "$env:TEMP\7zip-latest.exe" -Force -ErrorAction SilentlyContinue

Write-Host "7-Zip installation completed."
E5mLT3UkiG2dYgWKavHEzyhE5

Installing using a PowerShell script

Alternatively, you can create a PowerShell script using the code snippet above. For instance, I’ve created and save a script at “C:\Scripts\install.ps1″.

TqBHojDIQrVvPhwE7pTTg5YGA

Next, launch PowerShell (Terminal) as an administrator and run the script using either the call operator or dot notation.

& "C:\Scripts\install.ps1"
PEPE4ZT3JloUABbkaz6yEyuJx

PowerShell execution policy

In some cases, when trying to run a PowerShell script from the PowerShell console, you received this error message: “File cannot be loaded because running scripts is disabled on this system” or “The file is not digitally signed. You cannot run this script on the current system“

YBgwuyfTm5aDhpkiPEWgPXj3u

Root cause: The Windows PowerShell execution policy is designed to block untrusted scripts from impacting your Windows client environment. These policies act as security settings that define the trust level for scripts executed in PowerShell. On client operating systems, the default execution policy is set to Restricted, which stops Windows PowerShell commands and scripts from running.

To resolve the problem, set the execution policy using the following command:

Set-ExecutionPolicy RemoteSigned

You’ll see a security risk warning. Type “A” when prompted to proceed.

PS C:\> Set-ExecutionPolicy RemoteSigned

Execution Policy Change
The execution policy helps protect you from scripts that you do not trust. Changing the execution policy 
might expose you to the security risks described in the about_Execution_Policies help topic at
https:/go.microsoft.com/fwlink/?LinkID=135170. Do you want to change the execution policy?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): A
PS C:\>
PS C:\> Get-ExecutionPolicy
RemoteSigned

Read more: Details about the PowerShell execution policy.

Use Cases

  • Automated software deployment
  • Scheduled automatic updates
  • Silent install for non-technical users
  • Alternative to winget or manual downloads
Previous Post

alfaview

Next Post

How To Install WinRAR Using PowerShell

Related Posts

Air Explorer

How To Install PowerShell Silently on Windows

How To Install Inkscape Using PowerShell

How To Install Discord Using PowerShell

AIMP

How To Install qBittorrent Using PowerShell

Popular Apps

•  Google Chrome 

•  Mozilla Firefox

•  Zoom Workplace

•  VLC Media Player

•  Acrobat Reader

•  Foxit PDF Reader

• TeamViewer

  • Home
  • About Us
  • Contact
  • Disclaimers
  • Privacy Policy
  • Terms and Conditions
No Result
View All Result
  • Home
  • Blog
  • Contact
  • About Us