Powershellでプリンターの状態を取得する

Powershellでプリンタの状態(ステータス)を取得するにはPrintManagementモジュールのGet-Printerを利用すればよい。

必要な環境

Powershll 3.0 以降

実行例

Get-Printerコマンドレットで特定のプリンタ名を指定してでCIMインスタンス*1を取得し、PrinterStatusを取得する*2。Normalなら利用できる状態のようだ。

PS D:\SandBox> (Get-Printer –Name "Microsoft XPS Document Writer").PrinterStatus
Normal

※実行環境は以下

PS D:\SandBox> "$((get-wmiobject win32_operatingsystem).caption) ($((get-wmiobject win32_operatingsystem).version))"
Microsoft Windows 10 Pro (10.0.14393)

PS D:\SandBox> $PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      14393  206     

参考

PrintManagementモジュールについて
CIMについて
  • Windows用語集 - WMI:ITpro CIM(Common Information Model)はWIM(Windows Management Instrumentation)の一部で監視情報のデータ構造を標準化するための形式、と理解した。
Powershellをバージョン指定で実行する方法について

余談

いつもの通り、英語版も含めてPowershellのレファレンスはクソなので何とかしてほしい。 PrinterStatusが取りうる値の範囲ぐらい明記してほしい。

*1:正確には「Microsoft.Management.Infrastructure.CimInstance#ROOT/StandardCimv2/MSFT_Printer」

*2:取得したインスタンスのメンバーを確認すると「ScriptProperty System.Object PrinterStatus」とあるので、PrintManagementのみの独自拡張なのだと思う