38

I want to update a machine's Powershell version. Can this be done through the command line?

My present need is to update from PS 4 to PS 5 on a Windows server 2012R2.

I guess there is no catch-all solution for all Windows versions and all PS versions mixed with caveats like PS4 requiring Dotnet4.5 so let's keep the question as simple as possible, like the first two sentences above.

1
  • I am manipulating my own question here, but with PS7?, 7.x? one can install through winget. So a moment ago i winget upgrade powershell and updated my PS 7.2.1 to 7.2.2. A dialogue appeared so it was not totally CLI.
    – LosManos
    Mar 18 at 11:12

8 Answers 8

43

Run this command : iex "& { $(irm https://aka.ms/install-powershell.ps1) } -UseMSI"

And then run the MSI with your parameters.

After this you need to update the modules.

Reference:

https://www.thomasmaurer.ch/2019/03/how-to-install-and-update-powershell-6/

https://www.thomasmaurer.ch/2019/02/update-powershellget-and-packagemanagement/

1
  • 1
    Here is possibly a way too as a dotnet global tool. dotnet tool install --global PowerShell I haven't tried it though.
    – LosManos
    Sep 26, 2019 at 12:30
22

Open Powershell as admin and type the following command:

iex "& { $(irm https://aka.ms/install-powershell.ps1) } -UseMSI"

If you want to update to the latest preview, add the -Preview argument at the end:

iex "& { $(irm https://aka.ms/install-powershell.ps1) } -UseMSI -Preview"
2
  • 1
    This is not for Windows PowerShell, this is for PowerShell Core (PSv6 and above)., when the OP is asking about Windows PowerShell 4x to 5x.
    – postanote
    Jun 14, 2020 at 22:16
  • That is for upgrading to PowerShell Core (e.g. PowerShell 6 and up). The OP was asking how to upgrade from PowerShell 4 to PowerShell 5.
    – Ants
    Feb 14, 2021 at 2:25
2

Here's a little trick using Chocolatey

#Install Chocolatey
#region
echo "Setting up Chocolatey software package manager"
New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT

Get-PackageProvider -Name chocolatey -Force

echo "Setting up Full Chocolatey Install"
Install-Package -Name Chocolatey -Force -ProviderName chocolatey
$chocopath = (Get-Package chocolatey | 
            ?{$_.Name -eq "chocolatey"} | 
                Select @{N="Source";E={((($a=($_.Source -split "\\"))[0..($a.length - 2)]) -join "\"),"Tools\chocolateyInstall" -join "\"}} | 
                    Select -ExpandProperty Source)
& $chocopath "upgrade all -y"
choco install chocolatey-core.extension --force

echo "Creating daily task to automatically upgrade Chocolatey packages"
# adapted from https://blogs.technet.microsoft.com/heyscriptingguy/2013/11/23/using-scheduled-tasks-and-scheduled-jobs-in-powershell/
$ScheduledJob = @{
    Name = "Chocolatey Daily Upgrade"
    ScriptBlock = {choco upgrade all -y}
    Trigger = New-JobTrigger -Daily -at 2am
    ScheduledJobOption = New-ScheduledJobOption -RunElevated -MultipleInstancePolicy StopExisting -RequireNetwork
}
Register-ScheduledJob @ScheduledJob
#endregion

#Update Powershell
#region
$ErrorActionPreference = "silentlycontinue"

$PSVersionTable.PSVersion
choco install powershell -y
choco upgrade powershell -y

$ErrorActionPreference = "continue"
#endregion
3
2

You have to write your own script to do this. The is nothing pre-written that will do this for you with no effort from you. Downloading and installing files from the web is a very common practice. There are lots of online instructions and videos on how to do this.

Translation:

  1. You have to go to the URL Alexandr points you to.
  2. Click download to go to the next page to get to the direct URL and save that link.
  3. Then use the PowerShell web cmdlets, to download that file
  4. Then use the cmdlets to start an install or silent install.

There many examples on the web on the topic of how to download files from the web. Even pre-built samples that you can review and tweak for your effort.

See the MS PowerShell Gallery as your starting point.

Or look at the PowerShell built-in and or online help for examples.

# Get parameters, examples, full and Online help for a cmdlet or function

(Get-Command -Name Invoke-WebRequest).Parameters
Get-help -Name Invoke-WebRequest -Examples
Get-help -Name Invoke-WebRequest -Full
Get-help -Name Invoke-WebRequest -Online

(Get-Command -Name Invoke-Command).Parameters
Get-help -Name Invoke-Command -Examples
Get-help -Name Invoke-Command -Full
Get-help -Name Invoke-Command -Online

(Get-Command -Name Start-Process).Parameters
Get-help -Name Start-Process -Examples
Get-help -Name Start-Process -Full
Get-help -Name Start-Process -Online

Update

The OP specifically asked for ...

My present need is to update from PS 4 to PS 5 on a Windows server 2012R2.

..., not PowerShell Core.

Though changing to another accepted answer is fine, and I am not here just to collect points, the answer that 'Ariel D' is not valid for Windows PowerShell updating.

That command, as stated in the reference article he points to is for PowerShell Core (PSv6 and beyond). That command will not update PSv4 to PSv5. It will directly install/Update PowerShell Core to the latest version.

Windows PowerShell requires full .Net, PSCore only requires .Net core. That command will install PowerShell Core that latest version, and that does not upgrade or replace Windows PowerShell.

Also to run PowerShell core its executable is pwsh.exe, not powershell.exe. This install will not change your Windows PowerShell shortcuts, menu options, et all to PowerShell Core. It will create a new icon/shortcut for it, leaving all the default Windows PowerShell icons/shortcuts/settings, et all.

To make pwsh be your default, there are several Windows Menu and registry hacks you will have to make.

Windows PowerShell and PSCore are two separate environments, designed to run side-by-side and PSCore does not yet have full compatibility with Windows PowerShell.

Lastly, depending on what you were doing in your Windows PowerShell script 5x and below, that may not work in PowerShell Core at all, due to the backward compatibility. So, you will need to refactor/rewrite them.

For Example on Windows with both WinPS and PSCore installed, Get-WmiObject will still come up as a cmdlet, but in PowerShell Core, that will fail, since PowerShell Core does not support those cmdlets.

Example:

$PSVersionTable.PSVersion

Major  Minor  Patch  PreReleaseLabel BuildLabel
-----  -----  -----  --------------- ----------
7      0      2

 Get-Command -Name '*WMI*' | Format-Table -AutoSize

CommandType Name                    Version      Source
----------- ----                    -------      ------
Function    Get-WmiClassKeyProperty 1.3.6        PowerShellCookbook
Function    Search-WmiNamespace     1.3.6        PowerShellCookbook
Cmdlet      Get-WmiObject           3.1.0.0      Microsoft.PowerShell.Management
Cmdlet      Invoke-WmiMethod        3.1.0.0      Microsoft.PowerShell.Management
Cmdlet      Register-WmiEvent       3.1.0.0      Microsoft.PowerShell.Management
Cmdlet      Remove-WmiObject        3.1.0.0      Microsoft.PowerShell.Management
Cmdlet      Set-WmiInstance         3.1.0.0      Microsoft.PowerShell.Management
Application nvwmi64.exe             0.0.0.0      C:\Windows\system32\nvwmi64.exe
Application WMIADAP.exe             10.0.18362.1 C:\Windows\System32\Wbem\WMIADAP.exe
Application WmiApSrv.exe            10.0.18362.1 C:\Windows\System32\Wbem\WmiApSrv.exe
Application WMIC.exe                10.0.18362.1 C:\Windows\System32\Wbem\WMIC.exe
Application WmiMgmt.msc             0.0.0.0      C:\Windows\system32\WmiMgmt.msc
Application WmiPrvSE.exe            10.0.18362.1 C:\Windows\System32\Wbem\WmiPrvSE.exe

 Get-WmiObject -class Win32_OperatingSystem | Select-Object -Property Caption
Get-WmiObject: The term 'Get-WmiObject' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
 Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object -Property Caption

Caption
-------
Microsoft Windows 10 Pro

Also, some of the aliases you are used to in Windows PowerShell also don't exist in PowerShell Core.

For Example:

Windows PowerShell

 $PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      18362  752


 Get-Alias -Definition Invoke-WebRequest

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           curl -> Invoke-WebRequest
Alias           iwr -> Invoke-WebRequest
Alias           wget -> Invoke-WebRequest


 Get-Alias -Name curl

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           curl -> Invoke-WebRequest

PowerShell Core

$PSVersionTable.PSVersion

Major  Minor  Patch  PreReleaseLabel BuildLabel
-----  -----  -----  --------------- ----------
7      0      2

 Get-Alias -Definition Invoke-WebRequest

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           iwr -> Invoke-WebRequest

 Get-Alias -Name curl
Get-Alias: This command cannot find a matching alias because an alias with the name 'curl' does not exist.
 Get-Command -Name 'curl'

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Application     curl.exe                                           7.55.1.0   C:\Windows\system32\curl.exe
1
  • Not really sure why the down vote on my response, as there is noting invalid about it, but whatever,
    – postanote
    Jun 15, 2020 at 20:48
1

You should download WMF 5.0 from here. Then run a command from cmd:

wusa.exe D:\W2K12-KB3134759-x64.msu

or where you've downloaded package.

0
1

Albeit not the PS version the OP asked for; winget is doing this.
winget install --name PowerShell --exact

1
1

You can use WinGet to update packages like powersheel.

Introduction to WinGet https://docs.microsoft.com/fr-fr/windows/package-manager/winget/

Upgrade command doc https://docs.microsoft.com/fr-fr/windows/package-manager/winget/upgrade

To update all packages using WinGet, type winget upgrade --all in terminal as admin.

0

Cheat Sheet - Upgrade to PS 5.1 Easy as 123 - Estimated time to complete - 30 minutes

1.) Install, Verify .NET Framework is 4.5.2 or newer if not Download - Install Version 4.8 here: https://go.microsoft.com/fwlink/?linkid=2088631 and Reboot

2.) Install Windows Management Framework 5.1 https://go.microsoft.com/fwlink/?linkid=839516 - Pick your OS - Install - Reboot

3.) Open Powershell Console and verify updated to 5.1 enter $host or $PSVersionTable.PSVersion

1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Jun 10 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.