Update Powershell through command line
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/
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"
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:
- You have to go to the URL Alexandr points you to.
- Click download to go to the next page to get to the direct URL and save that link.
- Then use the PowerShell web cmdlets, to download that file
- 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.
- https://www.powershellgallery.com
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