在PowerShell中如何打印文件

打印文件源代码:
我们需要在系统上安装了Office Word

$wordfile = "c:\test.doc";
$objWord = New-Object -ComObject Word.Application
[void] $objWord.Documents.Open( $wordfile, $false, $true ) 
$objWord.ActiveDocument.PrintOut( )
$objWord.ActiveDocument.Close( $false )
$objWord.Quit( )

或者使用$objWord.ActivePrinter指定打印机打印

$wordfile = "c:\test.doc";
$objWord = New-Object -ComObject Word.Application
$defaultprinter = $objWord.ActivePrinter
$objWord.ActivePrinter = '打印机2'
[void] $objWord.Documents.Open( $wordfile, $false, $true )
$objWord.ActiveDocument.PrintOut( )
$objWord.ActiveDocument.Close( $false )
$objWord.ActivePrinter = $defaultprinter
$objWord.Quit( )

显示打印窗口

Add-Type -AssemblyName System.Windows.Forms
$printdialog = New-Object System.Windows.Forms.PrintDialog
$printdialog.AllowCurrentPage = $false
$printdialog.AllowPrintToFile = $false
$printdialog.AllowSelection = $false
$printdialog.AllowSomePages = $false
$printdialog.ShowNetwork = $false
$response = $printdialog.ShowDialog( ) # $response will be OK or Cancel
if ( $response -eq 'OK' ) { Write-Host 'Selected printer:' $printdialog.PrinterSettings.PrinterName }
日期:2020-04-11 23:04:34 来源:oir作者:oir