tech.guitarrapc.cóm

Technical updates

WingetでPowerShell(pwsh)をインストールする

PowerShell 7以降をWingetでインストールする方法を紹介します。対象OSはWindowsです。

Microsoftが推奨するインストール方法

MicrosoftはPowerShellをWingetでインストールすることを推奨しています。WingetはWindows11標準で利用できるパッケージマネージャです。何も導入をすることなくコマンドラインからサクッとインストールできます。これは近年のWindowsにおける劇的な体験改善だと感じます。1

Installing PowerShell on Windows | Microsoft Learn

インストール可能なPowerShell

インストール可能なPowerShellは、PowerShellとPowerShell Previewです。

$ winget search Microsoft.PowerShell
Name               Id                           Version   Source
-----------------------------------------------------------------
PowerShell         Microsoft.PowerShell         7.4.6.0   winget
PowerShell Preview Microsoft.PowerShell.Preview 7.5.0.101 winget

6.0.0.0~最新版までインストール可能なバージョンとして登録されています。検索時は--exact(あるいは-e)オプションをつけると厳密名で検索できておすすめです。

# × これはいや
$ winget show --id Microsoft.PowerShell --versions
# 〇 おすすめ
$ winget show -e --id Microsoft.PowerShell --versions
Found PowerShell [Microsoft.PowerShell]
Version
--------
7.4.6.0
7.4.5.0
7.4.4.0
7.4.3.0
7.4.2.0
7.4.1.0
7.4.0.0
7.3.11.0
...省略
6.0.4.0
6.0.3.0
6.0.2.0
6.0.1.0
6.0.0.0

PowerShellをインストール

WingetでPowerShellを追加します。PowerShellはMicrosoft.Powershellという名前で登録されていいます。今のバージョンは7.4です。

$ winget install --id Microsoft.PowerShell --source winget

指定したバージョンをインストールする場合は、--versionオプションを指定します。

# 7.4.6.0を指定
$ winget install --id Microsoft.PowerShell --source winget --version 7.4.6.0

Winget実行を非管理者ターミナルで実行するとUACのセキュリティプロンプトが表示されます。実行中のUACプロンプトを防ぐ場合、Windows 11 24H2で入ったsudoのインライン実行を使うといいでしょう。2

# sudoを使って非管理者ターミナルでも管理者昇格する
$ sudo winget install --id Microsoft.PowerShell --source winget --version 7.4.6.0
Found PowerShell [Microsoft.PowerShell] Version 7.4.6.0
This application is licensed to you by its owner.
Microsoft is not responsible for, nor does it grant any licenses to, third-party packages.
Successfully verified installer hash
Starting package install...
Successfully installed

プレビューバージョンのインストール

プレビューバージョンのPowerShellをインストールする場合は、Microsoft.PowerShell.Previewを指定します。今のバージョンは7.5です。

$ winget install --id Microsoft.PowerShell.Preview --source winget

アンインストール

アンインストールは次のコマンドで行います。

$ sudo winget uninstall --id Microsoft.Powershell
$ sudo winget uninstall --id Microsoft.PowerShell.Preview

まとめ

WingetでPowerShellのインストールも簡単にできます。Windows標準からできる意味でapt同様に使えるので、個人的にもおすすめです。


  1. ScoopやChocolateyを導入していない人に対して導入を促すより、Wingetを活用できたほうがいいでしょう。
  2. Sudo for Windowsの詳細は別記事で。Sudo for Windows | Microsoft Learn