Juan Manuel Sanchez
Juan Manuel Sanchez

Reputation: 79

Print command not working on specific printer if its not default

Im a little confused by this. If I use the Get-printer command I get the printers list. So I know the printer is there.

Get-Printer

Name                           ComputerName    Type         DriverName                PortName        Shared   Published  DeviceType     
----                           ------------    ----         ----------                --------        ------   ---------  ----------     
OneNote (Desktop)                              Local        Send to Microsoft OneN... nul:            False    False      Print          
UNBILLING                                      Local        Ghostscript PDF           unbilling       False    False      Print          
tiquete                                        Local        Generic / Text Only       com.printdis... False    False      Print          
OneNote for Windows 10                         Local        Microsoft Software Pri... Microsoft.Of... False    False      Print          
Microsoft XPS Document Writer                  Local        Microsoft XPS Document... PORTPROMPT:     False    False      Print          
Microsoft Print to PDF                         Local        Microsoft Print To PDF    PORTPROMPT:     False    False      Print          
Fax                                            Local        Microsoft Shared Fax D... SHRFAX:         False    False      Print          
EXPORT                                         Local        Generic / Text Only       C:\Users\Jua... False    False      Print          
EPSON TM-U220 Receipt                          Local        EPSON TM-U220 ReceiptE4   ESDPRT001       False    False      Print          
Adobe PDF                                      Local        Adobe PDF Converter       Documents\*.pdf False    False      Print    

But I make the command.

start-process -filepath "$root\UNB\FINAL_TEXTO\$archivo" -verb print | out-printer -Name "UNBILLING"  

The print job will not be sent if the printer is not set as DEFAULT. I don't want that, I want to send it to the specific printer not matter if its default or not.

Any help is appreciated.

Upvotes: 0

Views: 399

Answers (1)

Theo
Theo

Reputation: 61218

You need to first find the printer currently set as Default in order to change that (temporarily) to the printer you want to be used.

For that you can use CIM:

# get a list of printer objects and select the current Default and the 'UNBILLING' printers
$allPrinters    = Get-CimInstance -ClassName Win32_Printer
$defaultPrinter = $allPrinters | Where-Object { $_.Default -eq $true }
$printerToUse   = $allPrinters | Where-Object { $_.Name -eq 'UNBILLING' }

# temporarily set your printer as Default
$null = Invoke-CimMethod -InputObject $printerToUse -MethodName SetDefaultPrinter

# now start the application for thw $archivo filetype and invoke its 'Print' verb
Start-Process -FilePath "$root\UNB\FINAL_TEXTO\$archivo" -Verb print

# after printing, restore the previous default printer
$null = Invoke-CimMethod -InputObject $defaultPrinter -MethodName SetDefaultPrinter

Upvotes: 1

Related Questions