bin/7-ZipSetupManager.psm1

<#
    .NOTES
    --------------------------------------------------------------------------------
     Code generated by: SAPIEN Technologies, Inc., PowerShell Studio 2023 v5.8.219
     Generated on: 26.3.2023. 11:42
     Generated by: chx
    --------------------------------------------------------------------------------
    .DESCRIPTION
        Script generated by PowerShell Studio 2023
#>



    <#
        ===========================================================================
         Created with: SAPIEN Technologies, Inc., PowerShell Studio 2023 v5.8.219
         Created on: 26.3.2023. 00:58
         Created by: chxus
         Organization: CHXOFT
         Filename: 7-ZipSetupManager.psm1
        -------------------------------------------------------------------------
         Module Name: 7-ZipSetupManager
        ===========================================================================
    #>

    
    <#
        .EXTERNALHELP 7-ZipSetupManager.psm1-Help.xml
    #>

    function Get-7ZipDownloadLink
    {
        [CmdletBinding()]
        param
        (
            [Parameter(Mandatory = $false,
                       Position = 1,
                       HelpMessage = 'Get 32bit installer.')]
            [switch]$x86,
            [Parameter(Mandatory = $false,
                       Position = 2)]
            [switch]$GetFileNameOnly
        )
        $7zipDownloadWebpage = (Invoke-WebRequest https://7-zip.org/download.html).AllElements
        if (-not ($x86))
        {
            $setup = $7zipDownloadWebpage.href -like "*7z*-x64.exe"
            if ($setup.Count -gt 1)
            {
                $setup = $setup.Split([Environment]::NewLine) | Select-Object -First 1
            }
        }
        else
        {
            $setup = $7zipDownloadWebpage.href -like "*7z*.exe" -cnotlike "*x64*"
            if ($setup.Count -gt 1)
            {
                $setup = $setup.Split([Environment]::NewLine) | Select-Object -First 1
            }
        }
        $name = $setup.Split('/')
        $name = $name[1]
        $DownloadLink = "https://7-zip.org/$setup"
        if ($GetFileNameOnly)
        {
            return $name
        }
        else
        {
            return $DownloadLink
        }
    }
    
    <#
        .EXTERNALHELP 7-ZipSetupManager.psm1-Help.xml
    #>

    function Get-7ZipInstallationPath
    {
        [CmdletBinding()]
        param
        (
            [switch]$GetVersionOnly,
            [switch]$Property,
            [switch]$CheckAll
        )
        $GetPath = Microsoft.PowerShell.Management\Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\7-Zip*
        if (-not ($GetPath))
        {
            $GetPath = Microsoft.PowerShell.Management\Get-ChildItem HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\7-Zip*
        }
        if ($GetPath)
        {
            $GetPath = Microsoft.PowerShell.Management\Get-ItemProperty $GetPath.PsPath
            $InstallPath = $GetPath.InstallLocation
            $Version = $GetPath.DisplayVersion
            if ($CheckAll)
            {
                if ($GetPath.DisplayName -like '*x64*')
                {
                    $Pathx86 = Microsoft.PowerShell.Management\Get-ChildItem HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\7-Zip*
                    if ($Pathx86)
                    {
                        $Pathx86 = Microsoft.PowerShell.Management\Get-ItemProperty $Pathx86.PsPath
                        $InstallPathx86 = $Pathx86.InstallLocation
                        $InstallPath = $InstallPath + "`n" + $InstallPathx86
                    }
                }
            }
            if ($Property)
            {
                return $GetPath
            }
            elseif ($GetVersionOnly)
            {
                return $Version
            }
            else
            {
                return $InstallPath
            }
        }
        else
        {
            return $false
        }
    }
    
    <#
        .EXTERNALHELP 7-ZipSetupManager.psm1-Help.xml
    #>

    function Get-7ZipSetup
    {
        [CmdletBinding()]
        param
        (
            [Parameter(Mandatory = $false,
                       Position = 1,
                       HelpMessage = 'Enter valid path to download.',
                       HelpMessageBaseName = 'Download path')]
            [AllowEmptyString()]
            [string]$Path,
            [Parameter(Position = 2)]
            [switch]$x86,
            [Parameter(Position = 3)]
            [switch]$AndStartInstallation,
            [Parameter(Mandatory = $false,
                       Position = 4,
                       DontShow = $true)]
            [switch]$Silent
        )
        
        # Get-7ZipDownloadLink
        if ($x86)
        {
            $DownloadLink = Get-7ZipDownloadLink -x86
            $FileName = Get-7ZipDownloadLink -GetFileNameOnly
        }
        else
        {
            $DownloadLink = Get-7ZipDownloadLink
            $FileName = Get-7ZipDownloadLink -GetFileNameOnly
        }
        $DownloadPath = (Microsoft.PowerShell.Management\Get-Location).Path + "\$FileName"
        if ($path)
        {
            if (Microsoft.PowerShell.Management\Resolve-Path -Path "$Path" -ErrorAction SilentlyContinue)
            {
                $DownloadPath = $Path + $FileName
            }
            else
            {
                return 'Path does not exist.'
            }
        }
        Microsoft.PowerShell.Utility\Invoke-WebRequest -Uri $DownloadLink -OutFile $DownloadPath -ErrorAction Stop
        if ($AndStartInstallation)
        {
            if (-not ($Silent))
            {
                Microsoft.PowerShell.Management\Start-Process -FilePath $DownloadPath -Wait
            }
            else
            {
                Microsoft.PowerShell.Management\Start-Process -FilePath $DownloadPath -ArgumentList "/S" -Wait
            }
            return 'Installation complete.'
        }
        else
        {
            return "Downloaded to: $DownloadPath"
        }
    }
    
    <#
        .EXTERNALHELP 7-ZipSetupManager.psm1-Help.xml
    #>

    function Uninstall-7Zip
    {
        [CmdletBinding()]
        param
        (
            [switch]$Silent
        )
        # Get-7ZipInstallationPath
        $path = Get-7ZipInstallationPath -CheckAll
        if (-not ($path))
        {
            return '7-Zip is not installed.'
        }
        else
        {
            $check = $path | Microsoft.PowerShell.Utility\Measure-Object -Line
            if ($check.Lines -eq 2)
            {
                Microsoft.PowerShell.Utility\Write-Warning 'Found both 64bit and 32bit installations, removing 64bit first, to remove the 32bit also run this command again.'
            }
            $uninstall = Get-7ZipInstallationPath -Property
            $uninstall = $uninstall.UninstallString
            if ($Silent)
            {
                Microsoft.PowerShell.Management\Start-Process -FilePath $uninstall -ArgumentList "/S" -Wait -ErrorAction Stop
            }
            else
            {
                Microsoft.PowerShell.Management\Start-Process -FilePath $uninstall -Wait -ErrorAction Stop
            }
            $path = Get-7ZipInstallationPath -CheckAll
            if (-not ($path))
            {
                return '7-Zip for Windows uninstalled completely.'
            }
            else
            {
                $check = $path | Microsoft.PowerShell.Utility\Measure-Object -Line
                if ($check.Lines -eq 2)
                {
                    return 'Uninstall canceled or error occured, both versions present 64 and 32 bit.'
                }
                else
                {
                    return 'Uninstall canceled or there is 32bit version left to uninstall.'
                }
            }
        }
    }