I would like to print multiple pdf files using PowerShell.
I have tried multiple solutions available, but they did not work for me.
one solution I tried was the following:
get-childItem "*.pdf" | sort lastWriteTime | foreach-object {start-process $._Name -verb 'print'}
or also:
@("*.pdf") | %{gci $_} | sort Name | foreach-object {while (@(Get-Printer | Get-PrintJob).Count -gt 0) {Start-Sleep -S 3;write-Host . -NoNewline};Write-Host "";Write-Host $_.Name;start-process -FilePath $_.fullname -verb 'print';while (@(Get-Printer | Get-PrintJob).Count -eq 0) {Start-Sleep -S 1;write-Host . -NoNewline}}
The error I get is the following:
Start-Process: This command cannot be run due to the error: An error occurred trying to start process ‘C:Users123files123.pdf’ with working directory ‘C:Users123files’. No application is associated with the specified file for this operation.
I would like to avoid using Adobe Reader or other third-party apps to solve this issue./
To clarify: I would like to print it with an external printer(HP Printer) and not print the pdf again as another pdf file.
I would appreciate any hint.
Thank you!
3
Windows “Print” is the Microsoft legacy print text function from the days before windows. It will work with Text based files and in effect replaces “Copy portable file to port” but portable is a file.PRN not a file.PDF. This MAY work for printers that use “PDF Direct” or mPDF format (not a standard PDF).
We can call the functions to print text (or RTF or minimal DocX) to PDF (or any installed printer driver) very easily so for example at a command send the following line.
echo Hello World!>temp.txt & Write /pt temp.txt "Microsoft Print to PDF" "Microsoft Print to PDF" hello.pdf & timeout 4 & hello.pdf
If your using Acrobat as a PDF handler then the common approach is with (non recommended) kill switch:
$printer = Get-CimInstance -Class Win32_Printer -Filter "Name='Your Printer Name'"
Invoke-CimMethod -InputObject $printer -MethodName SetDefaultPrinter
$Directory = "\Your PDFs SourceFolder"
Get-ChildItem -path $Directory -recurse -include *.pdf |
ForEach-Object {
Start-Process -FilePath $_.fullname -Verb Print
Start-sleep 10
get-process Acrobat | stop-process -Force
remove-item $_.FullName -verbose -Force
}
As mentioned the default PDF handler in MS Windows is MS Edge (A Custom Chromium variation). It’s printing command ability is only to produce via headless an image.PNG or vector.PDF from HTML CSS styled files.
You could use it to command line print a “port.PDF” to any output port via VBS or JScript which are both native to the command shell. However that is considered “Old School” and very variable as it depends on timer variables. But is AFAIK the only “native out of box” solution.
Thus native PowerShell has no simple way to auto print a PDF via Edge. But if you pop-up a PDF page the user can quickly print that manually.
That is also a one line command with or without Edge.
Take the example Hello.pdf from above and send command via :
"C:Program FilesMicrosoftEdgeApplicationmsedge.exe" -app="%cd%hello.pdf" and we get a printable pop-up.
All of these commands can also be sent modified or not to console from PowerShell.
Finally there is Kiosk printing where you can first set the default printing device before calling a file in the web kiosk.
Usually for “silent” printing this still needs the user to press print.
"C:Program FilesMicrosoftEdgeApplicationmsedge.exe" --kiosk file://%cd%hello.pdf --edge-kiosk-type=fullscreen --no-first-run --kiosk-printing
ANSWER
I did Windows “Natively NOOB” send the generated PDF direct to the DEFAULT physical printer (thus could send any other PDF, or with more code change the default).
I strongly suggest you try to avoid this method as often slow or prone to timing failures when attempting faster. However in the spirit of disclosure here is the hardcoded version that will need adapt to variables via args[]
.
In the following each 1000 is 1 second delay
usage CScript PDF-defaultPrint.vbs
Dim objShell, WshShell
Set objShell = WScript.CreateObject ("WScript.shell")
objShell.run "cmd /c "&Chr(34)&Chr(34)&"C:Program FilesMicrosoftEdgeApplicationmsedge.exe"&chr(34)&" --app="&Chr(34)&"file://C:UsersnameDesktophello.pdf"&Chr(34)&Chr(34)&" "
WScript.Sleep 5000
Set WshShell = WScript.CreateObject ("WScript.shell")
WshShell.AppActivate "hello.pdf"
WScript.Sleep 10000
WshShell.SendKeys "^P"
WScript.Sleep 3000
WshShell.SendKeys "{enter}"
WScript.Sleep 500
Set objShell = Nothing
Set WshShell = Nothing
3
To print multiple PDF files using PowerShell on win11 you can use EDGE to handle the print job.
Ensure Microsoft Edge is Set as the Default PDF Handler:
- Go to Settings > Apps > Default apps.
- Search for .pdf and ensure Edge is selected as the default application for PDF files.
Use PowerShell to use Edge for Printing:
$pdfFiles = Get-ChildItem -Path "C:Users123files" -Filter "*.pdf" | Sort-Object LastWriteTime
foreach ($file in $pdfFiles) {
Write-Host "Printing $($file.Name)"
Start-Process "msedge.exe" -ArgumentList "--headless", "--print-to-pdf-no-header", "--print", "`"$($file.FullName)`"" -NoNewWindow -Wait
Start-Sleep -Seconds 3
}
Write-Host "Printing."
As for the error that you are getting. That is most likely related to you not having a Default PDF handler set up in the settings of your Win11 installation.
2