Windows11のディスプレイ設定をPowerShellで変更する

Windows11で、PowerShellを使ってディスプレイを有効化・無効化したり、解像度を変更したりできるようにした時のメモです。

動機

Chromebookを買って、Chromeリモートデスクトップ(長いので、以降CRDとします)を使い始めました。

CRDでは、接続先のPCがマルチディスプレイの場合はマルチのまま表示されます。
我が家の環境の場合、ChromebookのディスプレイでCRDをフルスクリーン表示して使うには、接続先のPCに対して以下のような設定をすることになります。
・外部ディスプレイをオフにしてシングルディスプレイ表示にする。
・画面解像度を1920*1080にする。
※接続先PCのネイティブ解像度は1920*1200、CRD接続元であるChromebookは1920*1080のため。
逆に、自宅でPCを使う場合はこの設定を元に戻す必要があります。

これが地味に面倒なので、スクリプト一発で変更したいなぁ、というのが今回の動機です。

準備

DisplayConfigのインストール

ここだけは管理者権限で起動したPowerShellを使います。以下のコマンドでDisplayConfigをインストールします。
本記事投稿時点では、バージョン5.2.1が導入されました。

Install-Module DisplayConfig

DisplayConfigのプロジェクトサイトは以下です。readmeがあります。

GitHub - MartinGC94/DisplayConfig: PowerShell module for configuring Windows display settings
PowerShell module for configuring Windows display settings - MartinGC94/DisplayConfig

コマンドの確認

ディスプレイ操作コマンドをいくつか記載します。一般ユーザ権限のPowerShellで実行できます。
ここに記載したもの以外にも多数のコマンドがあります。「Get-Command -Module DisplayConfig」を実行するとコマンド一覧を表示できます。

外部ディスプレイを有効化する

Enable-Displayコマンドを使います。-DisplayIdToDisableオプションで無効化も同時にできます。

書式

Enable-Display [-AsClone <System.Management.Automation.SwitchParameter>] [-DisplayId] <ディスプレイID> [-DisplayIdToDisable <ディスプレイID>] [<CommonParameters>]

実行例

ディスプレイ2を有効にする。

Enable-Display -DisplayId 2

カンマ区切りで複数ディスプレイ指定も可能。すでに有効なディスプレイに対してEnable-Displayを実行してもエラーにはならない。

Enable-Display -DisplayId 1,2

ディスプレイ2を有効にして、ディスプレイ1を無効にする。

Enable-Display -DisplayId 2 -DisplayIdToDisable 1

外部ディスプレイを無効化する

Disable-Displayコマンドを使います。

書式
2種類ありますが、無効化するだけなら1個目の書式で事足ります。

Disable-Display [-DisplayId] <ディスプレイID> [<CommonParameters>]

Disable-Display -DisplayConfig <MartinGC94.DisplayConfig.API.DisplayConfig> [-DisplayId] <ディスプレイID> [<CommonParameters>]

実行例

ディスプレイ2を無効にする。

Disable-Display -DisplayId 2

ディスプレイ解像度を指定する

Set-DisplayResolutionコマンドを使います。

書式

Set-DisplayResolution [-AllowChanges <System.Management.Automation.SwitchParameter>] [-DisplayId] <ディスプレイID> [-DontSave <System.Management.Automation.SwitchParameter>] [-Height] <縦解像度> [-Width] <横解像度> [<CommonParameters>]

Set-DisplayResolution [-ChangeAspectRatio <System.Management.Automation.SwitchParameter>] -DisplayConfig <MartinGC94.DisplayConfig.API.DisplayConfig> [-DisplayId] <ディスプレイID> [-Height] <縦解像度> [-Width] <横解像度> [<CommonParameters>]

実行例

ディスプレイ1の解像度を1920*1080にする

Set-DisplayResolution -DisplayId 1 -Width 1920 -Height 1080

メインディスプレイを指定する

Set-DisplayPrimaryコマンドを使います。

書式

Set-DisplayPrimary [-DisplayId] <ディスプレイID> [-DontSave <System.Management.Automation.SwitchParameter>] [<CommonParameters>]

Set-DisplayPrimary -DisplayConfig <MartinGC94.DisplayConfig.API.DisplayConfig> [-DisplayId] <ディスプレイID> [<CommonParameters>]

実行例

ディスプレイ2をメインディスプレイにする

Set-DisplayPrimary -DisplayId 2

スクリプトを作る

CRD接続時に使えるようにスクリプトとしてまとめます。
以下の内容をそれぞれPowerShellスクリプトファイル(ファイル名○○.ps1)として保存します。
実行するときは「右クリック→PowerShellで実行」で起動します。

ディスプレイ1のみ有効にして、解像度を1920*1080にする

Enable-Display -DisplayId 1 -DisplayIdToDisable 2
Set-DisplayResolution -DisplayId 1 -Width 1920 -Height 1080

ディスプレイ1、2とも有効にして、ディスプレイ1の解像度を1920*1200にする

Enable-Display -DisplayId 1,2
Set-DisplayResolution -DisplayId 1 -Width 1920 -Height 1200

以上です。
今回はディスプレイの有効化・無効化と解像度だけのスクリプトを作りましたが、他にリフレッシュレートや回転など様々な操作が可能なので、工夫次第で便利に使えるコマンドだと思います。

コメント

タイトルとURLをコピーしました