前提
- OS: Windows 11 Pro 21H2 22000.675
- 現在のPowerShell: 5.1.22000.653
- アップデート先のPowerShell: 7.2.4
現象
久しぶりにPowerShellを使うマシンでPowerShellを起動した際、最新のPowerShellのインストールするように促された。なお、PowerShellの起動には、管理者特権で起動するBATファイルを使用していた。
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
新機能と改善のために最新の PowerShell をインストールしてください!https://aka.ms/PSWindows
PS D:\work>
以下は、管理者特権でPowerShellを起動するBATファイル。このファイルは環境変数PATHの定義してあるフォルダに置くものとする。ただし、v5.1系。v7系では起動コマンドが異なるので注意が必要。(後述)
powershell -NoProfile -ExecutionPolicy unrestricted -Command "Start-Process PowerShell.exe -Verb runas"
現在のPowerShellのバージョンの確認
まず自分のpowershellのバージョンを確認する。v5.1系であることが分かる。
- 参考サイト: Windows PowerShell のインストール
PS D:\work> $PSVersionTable
Name Value
---- -----
PSVersion 7.2.4
PSEdition Core
GitCommitId 7.2.4
OS Microsoft Windows 10.0.22000
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
最新のPowerShellのバージョンを確認してインストールする
wingetでPowerShellの最新バージョンを確認すると7.2以降とのことなので、それを入れる。
PS D:\work> winget search Microsoft.PowerShell
名前 ID バージョン ソース
------------------------------------------------------------------
PowerShell Microsoft.PowerShell 7.2.4.0 winget
PowerShell Preview Microsoft.PowerShell.Preview 7.3.0.3 winget
インストール実行
winget install --id Microsoft.Powershell --source winget
管理者特権で起動するBATファイルの修正
参考サイトによると、以下の話なので、BATファイルを書き換える。具体的には、"powershell.exe"を"pwsh.exe"にする。
Windows PowerShell では、PowerShell の実行可能ファイルには powershell.exe という名前が付けられます。バージョン 6 以降では、実行可能ファイルには pwsh.exe という名前が付けられます。新しい名前を使用すると、両方のバージョンのサイド バイ サイド実行をサポートしやすくなります。
powershell -NoProfile -ExecutionPolicy unrestricted -Command "Start-Process pwsh.exe -Verb runas"
PowerShellが最新版で起動する事を確認
前述のBATファイルを起動すると以下の表示の窓が出る。
PowerShell 7.2.4
Copyright (c) Microsoft Corporation.
https://aka.ms/powershell
Type 'help' to get help.
PS D:\work>
アップデート後のバージョンを確認
念のため、アップデート後のpowershellのバージョンを確認する。
PS D:\work> $PSVersionTable
Name Value
---- -----
PSVersion 7.2.4
PSEdition Core
GitCommitId 7.2.4
OS Microsoft Windows 10.0.22000
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0