Add-Printer -ConnectionName $printer; gives error cannot contain '\' characters. How do I properly use a connection name here?

0

I have multiple text documents with each line a shared printer in the format:

\\pserver.company.com\printername0
\\pserver.company.com\printername1
\\pserver.company.com\printername2

I want to automate the installation of these lists of printers, preferably with PowerShell. My goal is to get the content of the text and for each line add the printer by its connection name.

I have tried running the following and it says I can't use '\' the -ConnectionName MUST contain '\' characters, that's how windows does it right? I've tried as many was of pulling the strings out of the text file into a variable as I can think of but it boils down to:

    $PrinterList = Get-Content c:\list_of_printers.txt
    ForEach($printer in $printerlist) {Add-Printer -ConnectionName $printer}

When I run the above loop it gives me this:

add-printer : The specified server does not exist, or the server or printer name is invalid. Names may not contain ',' or '\' characters. At C:\importprinter.ps1:7 char:34 + ... oreach ($printer in $printers) {add-printer -ConnectionName $printer} + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (MSFT_Printer:ROOT/StandardCimv2/MSFT_Printer) [Add-Printer], CimException + FullyQualifiedErrorId : HRESULT 0x80070709,Add-Printer

When I open the shell and manually say:

    $printer = \\pserver.company.com\printername0
    Add-printer -ConnectionName $printer

It works just dandy.

powershell
network-printers
asked on Stack Overflow May 29, 2019 by joshberger • edited May 29, 2019 by joshberger

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0