Updating PowerShell latest version

Updating PowerShell latest version

Updating PowerShell in Visual Studio Code to the latest version should be a piece of cake. It is, but this site would hardly have a reason to exist if I didn’t run into problems. I read one the blog Tweakers that there would be a new version.

First of all, I ask Copilot for a script to show all versions.

# Function to check installed versions of .NET SDK, PowerShell, WinGet, and CLI tools
function Get-InstalledVersions {
        # Check .NET SDK versions
        $dotnetSDKs = dotnet --list-sdks
        Write-Host "Installed .NET SDKs:"
        Write-Host $dotnetSDKs
    
        # Check PowerShell version
        $powershellVersion = $PSVersionTable.PSVersion
        Write-Host "Installed PowerShell Version:"
        Write-Host $powershellVersion
    
        # Check WinGet version
        $wingetVersion = winget --version
        Write-Host "Installed WinGet Version:"
        Write-Host $wingetVersion
    }
    
    # Call the function to display the versions
    Get-InstalledVersions

The output is as following.

Installed .NET SDKs:
9.0.105 [C:\Program Files\dotnet\sdk] 9.0.203 [C:\Program Files\dotnet\sdk]
Installed PowerShell Version:
7.5.0
Installed WinGet Version:
v1.10.390

When we start Visual Studio, the following message appears on the screen.

If we click Yes, Visual Studio Code redirects me to this PowerShell site. So I wondered if we couldn’t automate this. Of course, we ask Copilot. Naturally, the answer was yes. You can do this via the following code.

winget install --id Microsoft.Powershell --version $latestVersion

We are now encountering a known problem. This problem seems to occur with every version upgrade. For example, you can read it on this site. There are various possibilities. After some searching, I modified Copilot’s suggestion with winget upgrade.

function Update-PowerShell {
        # Fetch the latest PowerShell version from GitHub
        $latestVersionInfo = Invoke-RestMethod -Uri "https://api.github.com/repos/PowerShell/PowerShell/releases/latest"
        $latestVersion = $latestVersionInfo.tag_name.TrimStart("v")
    
        # Check the current PowerShell version
        $currentVersion = $PSVersionTable.PSVersion
        Write-Host "Current PowerShell Version: $currentVersion"
    
        # Compare versions
        if ($currentVersion -lt [version]$latestVersion) {
            Write-Host "Updating PowerShell to version $latestVersion..."
    
            # Install the latest version using Winget
            winget upgrade --id Microsoft.PowerShell --version $latestVersion
    
            Write-Host "PowerShell updated to version $latestVersion. Please restart your terminal."
        } else {
            Write-Host "You already have the latest version of PowerShell installed."
        }
    }
    
    # Call the function to check and update PowerShell
    Update-PowerShell

Unfortunately, this solution didn’t work (anymore). So what to do then? This site, however, provided the solution. The new function then becomes.

Solution updating PowerShell to latest version

function Update-PowerShell {
        # Fetch the latest PowerShell version from GitHub
        $latestVersionInfo = Invoke-RestMethod -Uri "https://api.github.com/repos/PowerShell/PowerShell/releases/latest"
        $latestVersion = $latestVersionInfo.tag_name.TrimStart("v")
    
        # Check the current PowerShell version
        $currentVersion = $PSVersionTable.PSVersion
        Write-Host "Current PowerShell Version: $currentVersion"
    
        # Compare versions
        if ($currentVersion -lt [version]$latestVersion) {
            Write-Host "Updating PowerShell to version $latestVersion..."
    
            # Install the latest version using Winget
            winget install --id Microsoft.PowerShell --source winget
    
            Write-Host "PowerShell updated to version $latestVersion. Please restart your terminal."
        } else {
            Write-Host "You already have the latest version of PowerShell installed."
        }
    }
    
    # Call the function to check and update PowerShell
    Update-PowerShell

The output of the first function now indicates.

Get-InstalledVersions

Installed .NET SDKs:
9.0.105 [C:\Program Files\dotnet\sdk] 9.0.203 [C:\Program Files\dotnet\sdk]
Installed PowerShell Version:
7.5.1
Installed WinGet Version:
v1.10.390

And if we run the update function again, the latest version is detected. Nice!

Update-PowerShell
Current PowerShell Version: 7.5.1
You already have the latest version of PowerShell installed.

During the analysis and attempts to solve the problem, I also removed PowerShell 7.5.0 using the following code.

function Force-ReinstallPowerShell {
        # Uninstall existing PowerShell
        Write-Host "Uninstalling existing PowerShell..."
        winget uninstall --id Microsoft.Powershell -e
    
        # Wait for uninstallation to complete
        Start-Sleep -Seconds 10
    
        # Install the latest version of PowerShell
        Write-Host "Installing the latest version of PowerShell..."
        winget install --id Microsoft.Powershell -e
    
        Write-Host "PowerShell has been reinstalled. Please restart your terminal."
    }
    
# Call the function to force reinstall PowerShell
Force-ReinstallPowerShell

After I restarted Visual Studio Code, there was a message that PowerShell 7 could not be found in the environment variable. And no matter what I did, PowerShell 5.x was the default. To complete this blog post, I am also adding a function that fixes this. You must first have PowerShell 7.5.1 correctly installed.

function Set-PowerShell7AsDefault {
        $newPath = "C:\Program Files\PowerShell\7"
        $currentPath = [System.Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::Machine)
    
        if ($currentPath -notcontains $newPath) {
            $updatedPath = "$newPath;$currentPath"
            [System.Environment]::SetEnvironmentVariable("Path", $updatedPath, [System.EnvironmentVariableTarget]::Machine)
            Write-Host "Environment variable updated. Please restart your system for changes to take effect."
        } else {
            Write-Host "PowerShell 7 path already exists in environment variables."
        }
    }
    
    # Call the function to update environment variables
    Set-PowerShell7AsDefault

We are once again spending some time with PowerShell to solve a problem due to Microsoft’s winget. Hopefully, other people who have the same problem can benefit from this blog.

A Dutch version of this blog post exists here.

0 Shares:
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like