LoginSignup
10
18

More than 5 years have passed since last update.

PowerShellを使って既定のプリンターの変更と用紙設定の変更

Last updated at Posted at 2019-01-10

PowerShellを使って印刷するスクリプトを書いた。

印刷
Start-Process \.printingfile.pdf -Verb Print

このスクリプトでは

  • 規定のプリンタ
  • 既定の用紙設定

で印刷し続けるだけ。

PowerShellで以下のことをやりたい

  • 既定のプリンターの変更
  • 既定の用紙設定の変更
    • 用紙サイズ
    • 印刷の向き

環境

  • Windows10 Pro/Home
  • PowerShell v5.1

既定のプリンターの変更

下記のコマンドを走らせれば一発。

既定のプリンターの変更
$Printers=Get-WmiObject Win32_Printer
$Printer=$Printers | Where-Object Name -eq "Canon Generic Plus LIPSLX"
# Powershell v2.0
# $Printer=$Printers | Where-Object {$_.Name -eq "Canon Generic Plus LIPSLX"}
$Printer.SetDefaultPrinter()

Windows10の設定でプリンターとスキャナーを見てみましょう。

pp04.png

設定されました。

用紙サイズの変更

A4からB5に変更します。

用紙サイズ変更
$Printers=Get-WmiObject Win32_Printer
$Printer=$Printers | Where-Object Name -eq "Canon Generic Plus LIPSLX"
# 基本設定のPaperTypeを変更
$Printer.DefaultPaperType="B5"
# 更新
$Printer.Put()

プリンターのプロパティの基本設定を開いてみましょう。

pp00.png

原稿サイズがB5になっています。

pp01.png

用紙の向きの変更

Page_Orientation
# 標準設定の用紙設定をXML出力
$p=Get-PrintConfiguration -PrinterName "Canon Generic Plus LIPSLX"
$p.PrintTicketXML | Out-File psettings.xml -Encoding default
$ps=get-content .\psettings.xml
# 用紙の向き 縦(Portrait)から横 Landscapeに変更
$ps -replace 'Portrait','Landscape' | out-file ppsettngs.xml -Encoding default
# 変更した用紙設定をセット
Set-PrintConfiguration -PrinterName "Canon Generic Plus LIPSLX" -PrintTicketXml (Get-Content .\ppsettngs.xml -Raw) 

実際に変更されているか確認してみます。
プリンタープロパティ > 詳細設定 > 標準の設定 を開いてみます。

pp03.png

印刷の向きが横になっています。
成功です。

pp02.png

原稿サイズがA4のままですが、基本設定の原稿サイズB5を優先して実行するので問題ありません。

用紙トレイの変更

用紙トレイを選択すると用紙サイズは無視されます。
一般的に下記のように設定されていることが多いです。

  • Auto
    • 自動
  • Manual
    • 手差し
  • Upper
    • トレイ1
  • Middle
    • トレイ2
  • Bottom
    • トレイ3

用紙の向きと同じように Set-printConfigurationを使って設定します。

Select_Tray
# 標準設定の用紙設定をXML出力
$p=Get-PrintConfiguration -PrinterName "Canon Generic Plus LIPSLX"
$p.PrintTicketXML | Out-File psettings.xml -Encoding default
$ps=get-content .\psettings.xml
# トレイの変更
$ps -replace 'Auto','Manual' | out-file ppsettngs.xml -Encoding default
# 変更した用紙設定をセット
Set-PrintConfiguration -PrinterName "Canon Generic Plus LIPSLX" -PrintTicketXml (Get-Content .\ppsettngs.xml -Raw) 

トレイがUser0000000261等の数値で指定されているプリンターもあります。
SHARP MXシリーズがそうでした。

もし動かなった場合、下記のようにPrintCapabilitiesXMLを出力して眺めてみて下さい。

使える設定を見る
$p=Get-PrintConfiguration -PrinterName "Canon Generic Plus LIPSLX"
$p.PrintCapabilitiesXML | out-file pc.xml
# Open
.\pc.xml

細かいお話

  • Windowsのプリンターの設定は基本設定が優先。
  • WMIObjectのWin32_Printerを使わないと基本設定の方の用紙設定が設定できない。
  • Win32_Printerではトレイ、用紙の向き等は設定できない。
  • Set-PrintConfigurationを使えばトレイ、用紙の向き(縦・横)の設定が可能
  • PowerShell v2.0しか使えない方はWin32_PrinterConfigurationを使うと幸せになれるかもしれない
10
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
18