Using PSReleaseTools to install latest PowerShell v6 release on Mac

I have been throughly enjoying the use of PowerShell on my new MacBook since it arrived a few months ago. Each release gets better and better. One thing that annoyed me was constantly having to install the latest release from Github. Luckily, Jeff Hicks created a nifty module for doing that named PSReleaseTools. While this is a great tool for grabbing the latest PowerShell v6 package, it does not actually install the package on your machine.

For this reason I went ahead and created a small function to leverage PSReleaseTools and the Mac command-line tool Installpkg to somewhat automate the process of grabbing the latest version of PS and installing it. I say “somewhat” because it appears installpkg requires you to use sudo when installing a package, so that is part of the function. Keep in mind I threw this together this morning so it does not have much error checking or best practices used and there is much to be improved. It obviously requires you install Installpkg, which you can download here https://github.com/henri/installpkg.
#requires -Modules PSReleaseTools

<#
.SYNOPSIS
    Compares local PowerShell version to Github latest release, installs using installpkg
#>
function Install-PowerShellonMac {
    [CmdletBinding()]

    param(
    )

    begin 
    {
        if ($IsOSX -eq $False)
        {
            Write-Error -Message 'This function only runs on OS X'
            break
        }
    }      
    process 
    {
        # Compare local PS and latest release
        $CompareVersion = Get-PSReleaseCurrent | Where-Object {$_.Version -ne $_.LocalVersion} | Select-Object Version,LocalVersion
        # If local version different than latest prompt user to install
        if ($CompareVersion)
        {
            Write-Output "$CompareVersion"
            Read-Host "Press Enter to install latest PowerShell release or CTL + C to exit"

            ##Download PS and install using installpkg
            $FileName = Get-PSReleaseAsset -Family MacOS | Select-Object FileName -ExpandProperty FileName
            Get-PSReleaseAsset -Family MacOS | Save-PSReleaseAsset
            sudo installpkg $FileName
        }   
        # If local PS and latest release are same break
        elseif (!$CompareVersion)
        {
            Write-Output 'Your local version is the same as latest release'
            break
        }
    }
}

Comments are closed.