# requires Windows 10 / Windows Server 2016 or better
# choose a name for your new printer $printerName = 'PrintPDFUnattended' # choose a default path where the PDF is saved $PDFFilePath = "$env:temp\PDFResultFile.pdf" # choose whether you want to print a test page $TestPage = $true
# see whether the driver exists $ok = @(Get-PrinterDriver-Name"Microsoft Print to PDF"-ea0).Count -gt0 if (!$ok) { Write-Warning"Printer driver 'Microsoft Print to PDF' not available." Write-Warning"This driver ships with Windows 10 or Windows Server 2016." Write-Warning"If it is still not available, enable the 'Printing-PrintToPDFServices-Features'" Write-Warning"Example: Enable-WindowsOptionalFeature -Online -FeatureName Printing-PrintToPDFServices-Features" return }
# check whether port exists $port = Get-PrinterPort-Name$PDFFilePath-ErrorAction SilentlyContinue if ($port-eq$null) { # create printer port Add-PrinterPort-Name$PDFFilePath }
# add printer Add-Printer-DriverName"Microsoft Print to PDF"-Name$printerName-PortName$PDFFilePath
# print a test page to the printer if ($TestPage) { $printerObject = Get-CimInstance Win32_Printer -Filter"name LIKE '$printerName'" $null = $printerObject | Invoke-CimMethod-MethodName printtestpage Start-Sleep-Seconds1 Invoke-Item-Path$PDFFilePath }