Saturday, October 22, 2022

PowerShell: Installing the latest version of PowerShell on a Windows Client Host

Overview

This post will demonstrate how to install the latest version of PowerShell on a Windows client host. Before installing the latest version of PowerShell it is important to recognize that a given host can have multiple versions of PowerShell installed. It is possible to have PowerShell 5.x and 7.x running on the same host and in fact, this can be quite handy for certain development situations (backward compatibility). After installing the latest version of PowerShell, it will also be shown how to run the previous version of PowerShell (5.x) or the latest version of PowerShell (7.x)

Installing PowerShell

The Microsoft recommended way to install Powershell on Windows 10 and Windows 11 clients is using winget. Microsoft's documentation, Installing PowerShell on Windows, points out that winget is not available on Windows server variants:


To install the latest version of PowerShell: 

1. Launch a PowerShell console window with administrative privileges. 

2. Before installing the latest version of PowerShell, displays the $host variable's Version property using the PowerShell console window.

Notice above that the version of PowerShell installed is 5.1.608 which is not the latest as 7x. is the latest.

3. To determine the latest version of PowerShell available for installation, from the PowerShell console invoke, winget search, as follows:

winget search Microsoft.PowerShell --source winget

Invoking the above command displays the following where both the latest stable version and the latest preview version of PowerShell are displayed:


4. To install the latest stable version of PowerShell, invoke winget with the following options:

winget install --id Microsoft.Powershell --source winget

Invoking the above command in a PowerShell console window displays the following:


Installing PowerShell 7.x will uninstall PowerShell 6.x. PowerShell 5.x can be installed on the same host as PowerShell 6.x and PowerShell 5.x can be installed on the same host as PowerShell 7.x.

Running the Latest Version of PowerShell

After installing the latest version of PowerShell, display $host.Version in the PowerShell console window one more time:



Notice in the screenshot above that the version of PowerShell running in the console window is 5.1 but the latest version, PowerShell 7.2.6 was installed.  Microsoft documentation, Migrating from Windows PowerShell 5.1 to PowerShell 7, points out that each version of PowerShell is installed in a different location:

To run a PowerShell console, the installed folder is not required.

To run PowerShell 5.x invoke:
  powershell.exe

To run PowerShell 6.x or 7.x invoke:
  pwsh.exe

Running a PowerShell 7.x Console

To run a PowerShell 7.x console window:
  • Click on the Windows key and type pwsh into the search textbox (see below)


  • Select "Run as administrator" to invoke a PowerShell 7.x console window running with administrative priveledges: 

Notice in the previous screenshot that the PowerShell console has identified that a new version of PowerShell is available. It is important to note that winget may be delayed by a few days before receiving the latest version of PowerShell. This post was written on October 22, 2022 and PowerShell 7.2.7 was built on October 17, 2022.

Getting the latest PowerShell MSI

Microsoft recommends winget for installing PowerShell (see: Installing PowerShell on Windows). As PowerShell is open source, Microsoft placed the code for PowerShell on Github, PowerShell. The page in Github containing all installable releases of PowerShell (as downloadable MSI files). The installers on Github include the latest release is PowerShell releases:


The screenshot above shows that PowerShell 7.2.7 was made available on Github on October 20, 2022, hence winget still considers PowerShell 7.2.6 as of October 22, 2022.

Visual Studio Code

At some point, a Microsoft tool will prompt you to upgrade to the latest version of PowerShell. Below is a dialog displayed by Visual Studio Code will editing a PowerShell file (*.ps1):


The prompt was seen on October 23, 2022, six days after the release was built (October 17, 2022).

Installing PowerShell Preview Version

If the latest preview version of PowerShell, invoke the following from a PowerShell console window running with administrative permissions:

winget install --id Microsoft.Powershell.Preview --source winget


No comments :

Post a Comment