Reputation: 11
I'm currently trying to write a powershell script that sets a users default printer, based on the value in a file.
When the value in the file is a networked printer \share\printername the script fails. I'm assuming this is due to the slashes "\". how should this be handled in powershell. I'm trying to determine if I need to add some special characters to escape the slashes.
##Read Default Printer value from file and set as new Default printer.
$path="\\share\profiles$\$env:username.V2\DefaultPrinter.txt"
$DefaultPrinterName=Get-Content $path
$DefaultPrinter = Get-Wmiobject -Query "Select * FROM WIN32_Printer WHERE Name like '$DefaultPrinterName'"
$DefaultPrinter.SetDefaultPrinter()
Upvotes: 1
Views: 1638
Reputation: 1
You have to specify the Name of the printer and not the path of printer.
In this case, please provide only "HP Color LaserJet 2600n" in the query and the query will return the correct output.
So the final query should be - "Select * from Win32_Printer where name like 'HP Color LaserJet 2600n'"
Upvotes: 0
Reputation: 72680
You just have to use double \
Example :
gwmi -Query "Select * from Win32_Printer where name like '\\\\192.168.2.49\\HP Color LaserJet 2600n'"
Location :
Name : \\192.168.2.49\HP Color LaserJet 2600n
PrinterState : 33554432
PrinterStatus : 3
ShareName : HPCL2600
SystemName : \\192.168.2.49
Upvotes: 1