Home > Windows にまつわる e.t.c.

PowerShell Core を CLI アップデートする


PowerShell 7.2 になって、Windows Update で PowerShell のアップデートが降ってくるようになったのですが、これがリリース直後に降ってくるわけでもないので、警告が出てウザい事がちょいちょいあります

 

winget がサポートされている環境の場合

GitHub から MSI を落としてくるとか、ストアでアップデートするとか色々アップデートする手はあるのですが、GUI 操作が面倒ですよね

そんな時は、winget コマンドでアップデートしちゃいましょう

winget install --id Microsoft.Powershell --source winget

 

コマンドラインだと、コピペ一発で簡単ですね :-)

 

winget がサポートされていない環境の場合

winget がサポートされていない環境では、Invoke-Expression コマンドレットを使って、MSI のダウンロード & インストールをします
(インストーラーが自動起動するので、.msi を叩く必要ありません)

Invoke-Expression "& { $(Invoke-RestMethod https://aka.ms/install-powershell.ps1) } -UseMSI"

 

関数として実装する場合

winget のサポートを判定してコマンドを使い分けるのであれば、関数化するのが良いでしょう

#################################################################
# PowerShell 更新
#################################################################
Function PsUpdate(){
    if(Get-Command winget -ea SilentlyContinue){
        winget install --id Microsoft.Powershell --source winget
    }
    else{
        Invoke-Expression "& { $(Invoke-RestMethod https://aka.ms/install-powershell.ps1) } -UseMSI"
    }
}

 

GitHub にも公開していますので、ご入用の方はどうぞ

https://github.com/MuraAtVwnet/PsUpdate
git@github.com:MuraAtVwnet/PsUpdate.git

 

参考情報

Windows への PowerShell のインストール - PowerShell | Microsoft Docs
https://docs.microsoft.com/ja-jp/powershell/scripting/install/installing-powershell-on-windows?WT.mc_id=WD-MVP-36880

winget ツールを使用したアプリケーションのインストールと管理 | Microsoft Docs
https://docs.microsoft.com/ja-jp/windows/package-manager/winget/?WT.mc_id=WD-MVP-36880

 

 

back.gif (1980 バイト)

home.gif (1907 バイト)

Copyright © MURA All rights reserved.