Functions/GenXdev.Software/Ensure7Zip.ps1
|
<############################################################################## Part of PowerShell module : GenXdev.Software Original cmdlet filename : Ensure7Zip.ps1 Original author : René Vaessen / GenXdev Version : 3.29.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" # 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}" } } 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)) { # verify administrative privileges are available if (-not (GenXdev\CurrentUserHasElevatedRights)) { $json = $PSBoundParameters | Microsoft.PowerShell.Utility\ConvertTo-Json -Compress GenXdev\Invoke-CommandElevated ([ScriptBlock]::Create(@" `$params = '$json' | ConvertFrom-Json -AsHashTable GenXdev\Ensure7Zip @params "@)) -JobDescription 'Installing 7-Zip'; return; } # 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...' 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...' Microsoft.WinGet.Client\Install-WinGetPackage -Id '7zip.7zip' -MatchOption EqualsCaseInsensitive # 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 } } } Microsoft.PowerShell.Utility\Write-Verbose '7-Zip is available on PATH.' } end { if ((Microsoft.PowerShell.Core\Get-Command '7z' -ErrorAction SilentlyContinue).Length -eq 0) { throw "7z installation failed" } } } |