32

I have two different revision of PowerShell in different machines. The local one have the following one:

Major Minor Build Revision
5 1 17763 1007

And the virtual machine has the following one:

Major Minor Build Revision
5 1 17763 771

As you can see that it has the same: Major, Minor, and Build values except the Revision values. I am not sure if it is behind the failure of the command:

Register-PSRepository -Name $RepoKeyName -SourceLocation $RepoKeyValue
    -PublishLocation $RepoKeyValue -InstallationPolicy Trusted -Verbose

The above snippet works fine on the local machine but not on the virtual machine and it fails in the virtual machine with the following error:

parameter 'SourceLocation' is an invalid Web Uri. Please ensure that it meets the Web Uri requirements.

And this is why I want to update the PowerShell in the virtual machine to the latest revision value. How to do it?

Toni
  • 1,555
  • 4
  • 15
  • 23
sajis997
  • 1,089
  • 1
  • 15
  • 29

5 Answers5

48

If you have Microsoft's winget app (Windows package manager), you can run the following command to update to the most recent version of PowerShell:

winget install Microsoft.PowerShell
sanampakuwal
  • 432
  • 5
  • 11
GaTechThomas
  • 5,421
  • 5
  • 43
  • 69
  • 5
    That's good to know, but to be clear: This applies only to [_PowerShell (Core) 7+_](https://github.com/PowerShell/PowerShell/blob/master/README.md), the install-on-demand cross-platform PowerShell edition, not the comes-with-the-OS Windows-only _Windows PowerShell_ edition, which cannot be updated _on demand_ (only indirectly, via Windows Update). – mklement0 Apr 29 '21 at 15:31
  • 10
    To update to PowerShell 7.2.0 from 7.1.5, I had to use `winget upgrade PowerShell` – brnlmrry Nov 08 '21 at 22:43
  • 2
    I had to explicitly state `winget upgrade Microsoft.PowerShell` to get 7.2.1 from 7.2.0. – eksortso Jan 18 '22 at 23:43
27
If you're running Windows 11 or have updated App Installer in Windows 10.

Update PowerShell using Windows Package Manager (winget)

winget upgrade Microsoft.PowerShell

You also can install PowerShell by using below command via winget

winget install Microsoft.PowerShell

Learn more:

sanampakuwal
  • 432
  • 5
  • 11
  • I had to use `winget install Microsoft.PowerShell` to upgrade from 7.2.5 to 7.2.6; `winget upgrade` reported "No applicable upgrade available." – A. R. Sep 20 '22 at 20:24
  • 1
    To be clear: This applies only to [_PowerShell (Core) 7+_](https://github.com/PowerShell/PowerShell/blob/master/README.md), the install-on-demand cross-platform PowerShell edition, not the comes-with-the-OS Windows-only _Windows PowerShell_ edition that the question is about. _Windows PowerShell_ can NOT be updated on demand (only indirectly, via Windows Update). Switching _editions_, from Windows PowerShell to PowerShell (Core) is more than just a fully backward-compatible update, and requires careful planning. – mklement0 Oct 18 '22 at 23:31
11

You can never update Windows PowerShell installations on demand - except, in the past, if you upgraded to a new major version, but v5.1 is the last version that will ever be released, given that Windows PowerShell is in maintenance-only will see no new development, unlike its successor, the cross-platform PowerShell (Core) 7+ edition.[1]

Note:

  • While switching to the PowerShell (Core) edition[1] - where all future development effort will go - is advisable in general, doing so is not something to be done casually and requires a deliberate decision:
    • PowerShell (Core) is mostly, but not fully backward-compatible with Windows PowerShell, and certain cmdlets are unavailable, except via a compatibility feature that has its limitations both in terms of performance and type fidelity.

    • PowerShell (Core) is installed alongside Windows PowerShell and has different CLI (pwsh.exe rather than powershell.exe) and different SDKs (see this answer); also, targeting PowerShell (Core) via PowerShell remoting requires explicit configuration - see this answer.


Windows PowerShell-specific considerations:

Revisions of v5.1 are delivered as part of Windows updates.

However, you can selectively update the PowerShellGet module, in which the problem-causing Register-PSRepository command is defined:

While you normally would just run Update-Module PowerShellGet, a different approach is required the first time, when switching from the bundled PowerShellGet module to the latest version from the PowerShell Gallery:

  • Open an elevated session (Run as Administrator).

  • Execute the following (add -Verbose to get detailed information):

      Install-Module PowerShellGet -Force
    

The -Force is to enable installation even though a module by that name is already installed; you may still see a prompt about downloading the NuGet package provider.
Note that the old PowerShellGet version will linger in a different location, but the new one will take precedence over it.

After this initial switch to the gallery-installed version, you'll be able to use
Update-Module PowerShellGet for future versions.


You can use the Get-Command cmdlet to discover a given command's module of origin; e.g.:

PS> (Get-Command Register-PSRepository).Module

ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Script     2.1.4                 PowerShellGet                       {Find-Command, Find-DscResource, Find-Module, Find-RoleCapability…}

[1] PowerShell (Core) 7+ versions can be updated on demand - however, as of v7.2.x, PowerShell (Core) doesn't come with Windows and initially requires manual installation. However, you can now install and update it via the Microsoft Store application or, programmatically, using winget.exe (which comes with the App Installer Microsoft Store application, which recent versions of Windows ship with):

  • Initial installation:

    winget install Microsoft.PowerShell
    
  • Later upgrade:

    winget upgrade Microsoft.PowerShell
    

Note: Use Microsoft.PowerShell.Preview to install / upgrade the latest preview version instead.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • I haven't tried this approach, not yet. But this seems the best answer so far, `winget install Microsoft.PowerShell` (as per other answers) says successful installation but `$PSVersionTable` doesn't get any further than `5.1.19041.revision` – rellampec Oct 18 '22 at 22:22
  • 1
    @rellampec, I've updated the footnote with more details on the `winget.exe` approach, but note that it only applies to the cross-platform, install-on-demand [_PowerShell (Core) 7+_](https://github.com/PowerShell/PowerShell/blob/master/README.md) edition. The question - and the focus of this answer - is the legacy, ships-with-Windows, in-maintenance-only-mode _Windows PowerShell_ edition, whose latest and _last-ever_ version is v5.1.x – mklement0 Oct 18 '22 at 22:53
9

Run the following command from command prompt wait till gets downloaded, and it will prompt to installation wizard follow the instructions to install it.

Invoke-Expression "& { $(irm https://aka.ms/install-powershell.ps1) } -UseMSI"
TylerH
  • 20,799
  • 66
  • 75
  • 101
Suresh B B
  • 1,387
  • 11
  • 13
  • I was curious what iex stood for (Invoke-Expression). Now I know. – Rich Jan 13 '22 at 21:31
  • 1
    To be clear: This applies only to [_PowerShell (Core) 7+_](https://github.com/PowerShell/PowerShell/blob/master/README.md), the install-on-demand cross-platform PowerShell edition, not the comes-with-the-OS Windows-only _Windows PowerShell_ edition that the question is about. _Windows PowerShell_ can NOT be updated on demand (only indirectly, via Windows Update). Switching _editions_, from Windows PowerShell to PowerShell (Core) is more than just a fully backward-compatible update, and requires careful planning. – mklement0 Oct 18 '22 at 23:32
0

Solution1: Go to this link:

https://github.com/PowerShell/PowerShell/releases/

Find Assets and click on Assets word.

Download and install .msi link.


Solution2:

Go to this link for download Windows Package Manager:

https://github.com/microsoft/winget-cli/releases

Find Assets and click on Assets word.

Download : Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle

Execute the downloaded file and click on update.

Open your command prompt or powershell and execute this command on it:

winget install Microsoft.PowerShell

If you have Mircrosoft.PowerShell execute this command:

winget upgrade Microsoft.PowerShell


For figure out your powershell version: execute host command in your powershell. enter image description here

M Komaei
  • 7,006
  • 2
  • 28
  • 34
  • To be clear: This applies only to [_PowerShell (Core) 7+_](https://github.com/PowerShell/PowerShell/blob/master/README.md), the install-on-demand cross-platform PowerShell edition, not the comes-with-the-OS Windows-only _Windows PowerShell_ edition that the question is about. _Windows PowerShell_ can NOT be updated on demand (only indirectly, via Windows Update). Switching _editions_, from Windows PowerShell to PowerShell (Core) is more than just a fully backward-compatible update, and requires careful planning. – mklement0 Oct 18 '22 at 23:33