Functions/GenXdev.Software/Ensure7Zip.ps1

<##############################################################################
Part of PowerShell module : GenXdev.Software
Original cmdlet filename : Ensure7Zip.ps1
Original author : René Vaessen / GenXdev
Version : 3.28.2026
################################################################################
Copyright (c) 2026 René Vaessen / GenXdev

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
################################################################################>


###############################################################################
<#
.SYNOPSIS
Ensures 7-Zip is installed and available on the PATH.

.DESCRIPTION
This function verifies if 7-Zip is installed on the system by checking both
the PATH and the default installation location. If not found, it attempts to
install 7-Zip via winget after obtaining user consent. Adds the 7-Zip
directory to the session PATH so that 7z.exe can be invoked directly.

.EXAMPLE
Ensure7Zip
7z x -y "-oC:\Output" archive.7z
#>

function Ensure7Zip {

    [CmdletBinding()]
    param(
        #######################################################################
        [Parameter(
            Mandatory = $false,
            HelpMessage = ('Automatically consent to 7-Zip installation ' +
                'and set persistent flag.')
        )]
        [switch] $AutoConsent,

        #######################################################################
        [Parameter(
            Mandatory = $false,
            HelpMessage = ('Automatically consent to third-party software ' +
                'installation and set persistent flag for all packages.')
        )]
        [switch] $AutoConsentAllPackages,
        #######################################################################
        [Parameter(
            Mandatory = $false,
            HelpMessage = ('Only auto consent during session')
        )]
        [switch]$SessionOnly
        #######################################################################
    )

    begin {

        Microsoft.PowerShell.Utility\Write-Verbose 'Ensuring 7-Zip is installed...'

        # default installation directory
        $sevenZipDir = "${env:ProgramFiles}\7-Zip"
        $sevenZipExe = "${sevenZipDir}\7z.exe"
    }

    process {

        # check if 7z is available in PATH
        if ((Microsoft.PowerShell.Core\Get-Command '7z' -ErrorAction SilentlyContinue).Length -eq 0) {

            # try the default installation location
            if (-not [IO.File]::Exists($sevenZipExe)) {

                # 7-Zip not found — need to install
                Microsoft.PowerShell.Utility\Write-Verbose '7-Zip not found, attempting installation...'
                Microsoft.PowerShell.Utility\Write-Host '7-Zip not found. Installing 7-Zip...'

                # verify winget is available for installation
                if ((Microsoft.PowerShell.Core\Get-Command winget -ErrorAction SilentlyContinue).Length -eq 0) {
                    throw '7-Zip is not installed and winget is not available. Please install 7-Zip or winget first.'
                }

                try {
                    # request consent before installing 7-Zip
                    $consent = GenXdev\Confirm-InstallationConsent `
                        -ApplicationName '7-Zip' `
                        -Source 'Winget' `
                        -Description ('Archive extraction and compression ' +
                            'utility required for processing archive files') `
                        -Publisher 'Igor Pavlov' `
                        -AutoConsent:$AutoConsent `
                        -AutoConsentAllPackages:$AutoConsentAllPackages

                    if (-not $consent) {
                        throw '7-Zip installation was denied by user.'
                    }

                    # install 7-Zip via winget
                    Microsoft.PowerShell.Utility\Write-Verbose 'Installing 7-Zip via winget...'
                    winget install 7zip

                    # verify installation succeeded
                    if (-not [IO.File]::Exists($sevenZipExe)) {
                        throw '7-Zip installation completed but 7z.exe was not found at the expected location.'
                    }

                    Microsoft.PowerShell.Utility\Write-Host '7-Zip installed successfully.'
                }
                catch {
                    Microsoft.PowerShell.Utility\Write-Error "Failed to install 7-Zip. Error: $PSItem"
                    throw
                }
            }

            # add the 7-Zip directory to the session PATH
            if ($env:Path -notlike "*${sevenZipDir}*") {
                $env:Path = "${sevenZipDir};${env:Path}"
                Microsoft.PowerShell.Utility\Write-Verbose "Added 7-Zip directory to PATH: ${sevenZipDir}"
            }
        }

        Microsoft.PowerShell.Utility\Write-Verbose '7-Zip is available on PATH.'
    }

    end {
    }
}