21

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.

20

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
  • 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 '19 at 12:30
12

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 '20 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 at 2:25
3

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
2
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

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 '20 at 20:48
0

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

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.