Published in Articles on by Michiel van Oosterhout ~ 6 min read

When it comes to software to handle archives, I agree with Scott Hanselman when he says that it's over and 7-Zip won. So let's not waste words, and just install it.


7-Zip icon
7-Zip icon

7-Zip installation

The Windows PowerShell script below downloads and installs 7-Zip on Windows 11 ARM.

(This script requires an elevated Windows PowerShell session.)

# Download the installer file
$downloadsPath = "$Env:USERPROFILE\Downloads"
$installerFileName = "7z2201-arm64.exe"
$installerFilePath = "$downloadsPath\$installerFileName"
$url = "https://www.7-zip.org/a/$installerFileName"
Invoke-WebRequest -Uri $url -OutFile $installerFilePath

# Ensure the program files directory exists
$programFilesDirectoryPath = "$Env:ProgramFiles\7-Zip"
if (-not (Test-Path -Path $programFilesDirectoryPath))
{
    $_ = New-Item -Force -ItemType Directory -Path $programFilesDirectoryPath
}

# Install 7-Zip
& $installerFilePath /S /D="$programFilesDirectoryPath"

# Add path to the PATH environment variable
$keyPath = "HKLM:\System\CurrentControlSet\Control\Session Manager\Environment"
$value = (Get-ItemProperty -Path $keyPath -Name "Path").Path.Trim(";") + ";%ProgramFiles%\7-Zip"
Set-ItemProperty -Path $keyPath -Name "Path" -Value $value

# Delete the installer
Start-Sleep -Seconds 2
Remove-Item -Force -Path $installerFilePath

The script downloads an installer from the official website, and runs it in silent mode using the /S command-line option. The installer automatically adds 7-Zip to the start menu. The script then adds the path to the installation directory to the PATH environment variable so that 7z.exe can be used in any Windows Command Processor or PowerShell session.

7-Zip configuration

Since we have installed Sysinternals we can use Process Monitor to monitor 7-Zip's access to the Windows Registry while we change some of its settings. Add these filters to only see how 7-Zip saves settings to the Windows Registry:

  • Process Name is 7zFM.exe
  • Operation is RegSetValue

With this we can find the mapping between 7-Zip settings and Windows Registry values, and use this information to automate the configuration using the Windows PowerShell script below.

# Define the flag values for the context menu items
$extractFiles = 1
$extractHere = 2
$extractToFolder = 4
$testArchive = 16
$openArchive = 32
$openArchiveSubMenu = 64
$addToArchive = 256
$addToArchive7z = 512
$compressAndEmail = 1024
$compressToArchive7zAndEmail = 2048
$addToArchiveZip = 4096
$compressToArchiveZipAndEmail = 8192
$7ZipcrcSha = 1073741824
$crcSha = 2147483648

# Define the registry values
$keys = @(
    @{
        Path = "HKCU:\Software\7-Zip\FM"
        Values = @(
            @{
                Name = "Toolbars"
                DisplayName = "View > Toolbars > Large Buttons"
                Value = 15
            }
            @{
                Name = "ShowDots"
                DisplayName = "Tools > Options... > Settings > Show `"..`" item"
                Value = $true
            }
            @{
                Name = "FullRow"
                DisplayName = "Tools > Options... > Settings > Full row select"
                Value = $true
            }
            @{
                Name = "ShowSystemMenu"
                DisplayName = "Tools > Options... > Settings > Show system menu"
                Value = $true
            }
        )
    }
    @{
        Path = "HKCU:\Software\7-Zip\Options"
        Values = @(
            @{
                Name = "MenuIcons"
                DisplayName = "Tools > Options... > 7-Zip > Icons in context menu"
                Value = $true
            }
            @{
                Name = "ContextMenu"
                DisplayName = "Tools > Options... > 7-Zip > Context menu items"
                Value = $extractHere + $extractToFolder + $addToArchiveZip
            }
        )
    }
)

foreach ($key in $keys)
{
    # Ensure registry key exists
    $path = $key.Path
    if (-not (Test-Path -Path $path))
    {
        $_ = New-Item -Force -ItemType Directory -Path $path
    }

    # Configure 7-Zip via registry values
    foreach ($value in $key.Values)
    {
        $displayName = $value.DisplayName

        $name = $value.Name
        $type = "DWord"
        $value = $value.Value
        if ($value -is [bool])
        {
            $value = [int]$value
        }
        elseif ($value -is [string])
        {
            $type = "String"
        }

        Write-Host "[$displayName]... " -NoNewline
        Set-ItemProperty -Path $path -Name $name -Value $value -Type $type
        Write-Host "OK"
    }
}

This script simply loops through some predefined paths and values and sets them in the Windows Registry. These are just some of the settings, and does not include the file association settings, because those require some addition code to set.

File type associations

The Windows PowerShell script below associates some supported file types with 7-Zip. You should adjust the list file types you want to associate with 7-Zip, this list is just given as an example.

# Used to increment a counter for file type associations
$counterPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer"
$counterName = "GlobalAssocChangedCounter"

# Define the file type classes
$classes = @(
    @{
        Extension = "7z"
        Description = "7-Zip archive"
        Icon = 0
    }
    @{
        Extension = "zip"
        Description = "ZIP archive"
        Icon = 1
    }
    @{
        Extension = "rar"
        Description = "RAR archive"
        Icon = 3
    }
)

# Name of commonly used value
    $default = "(Default)"

foreach ($class in $classes)
{
    $extension = $class.Extension
    $fileType = "7-Zip.$extension"
    $description = $class.Description
    $icon = "$Env:ProgramFiles\7-Zip\7z.dll,$($class.Icon)"
    $command = "`"$Env:ProgramFiles\7-Zip\7zFM.exe`" `"%1`""

    Write-Host "Associating 7-Zip with [$description] files... " -NoNewline

    # Create a key and value for the extension
    $path = "HKCU:\Software\Classes\.$extension"
    if (-Not (Test-Path -Path $path))
    {
        $key = New-Item -Force -ItemType Directory -Path $path
    }
    Set-ItemProperty -Path $path -Name $default -Value $fileType -Type "String"

    # Create a key for the file type
    $path = "HKCU:\Software\Classes\$fileType"
    if (-Not (Test-Path -Path $path))
    {
        $key = New-Item -Force -ItemType Directory -Path $path
    }
    Set-ItemProperty -Path $path -Name $default -Value $description -Type "String"

    # Create a key for the icon type
    $iconPath = "$path\DefaultIcon"
    if (-Not (Test-Path -Path $iconPath))
    {
        $key = New-Item -Force -ItemType Directory -Path $iconPath
    }
    Set-ItemProperty -Path $iconPath -Name $default -Value $icon -Type "String"

    # Create a key for the shell command
    foreach ($segment in ("shell","open","command"))
    {
        $path = "$path\$segment"
        if (-Not (Test-Path -Path $path))
        {
            $key = New-Item -Force -ItemType Directory -Path $path
        }
    }
    Set-ItemProperty -Path $path -Name $default -Value $command -Type "String"

    # Increment the counter
    $counterValue = (Get-ItemProperty -Path $counterPath -Name $counterName).$counterName
    Set-ItemProperty -Path $counterPath -Name $counterName -Value ($counterValue + 1)

    Write-Host "OK"
}

This script creates a consistent structure of keys and values in the Windows Registry such that files supported by 7-Zip get an icon in the Windows shell and can be opened in 7-Zip by double-clicking them.

The script increments a counter in the Windows Registry for each file type association. This is based on an analysis of 7-Zip's behavior. This might be a way to notify the Windows shell of the changes to the file types.

Overriding the .zip file type association

Windows Explorer will not automatically open .zip files with 7-Zip, because there is already another association for this file type. Instead Windows Explorer will ask which program to use:

.zip file type association in Windows 11
.zip file type association in Windows 11

When you select 7-Zip and choose to always open .zip files with 7-Zip, Windows Explorer will write this setting along with a hash to the Windows Registry. Since it is not possible for us to generate a valid hash, we cannot fully automate this file type association.

Summary

It's over and 7-Zip won and we can automate 100% of the installation and configuration of 7-Zip, and most of the file type associations for Windows Explorer as well, except for the .zip file type association, which must be changed using Windows Explorer itself.