Update Powershell through command line

4

2

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.

LosManos

Posted 2018-01-19T08:46:35.373

Reputation: 343

Answers

0

You have to write your own script to do this. The is noting 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 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 a install or silent install.

There many examples on the web on the topic of how to download file from the web. Even pre-built samples that you can review and tweak for you 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

postanote

Posted 2018-01-19T08:46:35.373

Reputation: 1 783

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

Remy van Tour

Posted 2018-01-19T08:46:35.373

Reputation: 180

It is a [trusted package(https://chocolatey.org/faq#what-is-a-trusted-package) but it is not maintained by Microsoft. Good information though. – LosManos – 2018-03-05T12:28:40.590

2

Open Powershell as admin and type the following command:

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

Ariel Ponce

Posted 2018-01-19T08:46:35.373

Reputation: 21

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.

Alexandr Kovalchuk

Posted 2018-01-19T08:46:35.373

Reputation: 354

1

Try: 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/

Ariel D

Posted 2018-01-19T08:46:35.373

Reputation: 11

Here is possibly a way too as a dotnet global tool. dotnet tool install --global PowerShell I haven't tried it though. – LosManos – 2019-09-26T12:30:29.270