New to powershell trying to add network printer but need to alos use credentials

I have been working on a script that allows me to add a network printer to a list of computers from a CSV and that network printer can only be added if credentials are put in when downloading it on a local machine in devices and printers. This is what I have but its not working i’m surely doing something wrong. Could anyone give me guidance on what im doing wrong or what would work better. I am not the GPO admin so this is the route I have to go.

$Credential = Get-Credential
$a = Import-Csv addprinter.csv -Header Computers
Foreach ( $Com in $a ) {{$PrinterPath = “\domain\name”
$net = new-Object -com WScript.Network
$net.AddWindowsPrinterConnection($PrinterPath)} $Com.Computers -DomainCredential $Credential -Force}

Can you be more specific on what you are trying to do? You want to add a network printer, for your user\profile, on multiple computers? Printers are per user, not per system, so this code if it were working would only add the printer for you on the the remote systems. You’re asking for assistance, but you haven’t specified exactly what issue you are having with the provided code.

$Credential = Get-Credential
 $a = Import-Csv addprinter.csv -Header Computers
 Foreach ( $Com in $a ) 
   {
     { # <-- Remove this...
      $PrinterPath = "\\domain\name"
      $net = new-Object -com WScript.Network 
      $net.AddWindowsPrinterConnection($PrinterPath)
     } # <-- And this.
     $Com.Computers -DomainCredential $Credential -Force # <-- Remove this.
   } 

You have an extra set of curly braces in your foreach. I have no idea what this line is “$Com.Computers -DomainCredential $Credential -Force” Where did you get that command from?

The new-object cmdlet doesn’t have a -credential parameter. Maybe you can try invoking the command?

$Credential = Get-Credential
$a = Import-Csv addprinter.csv -Header Computers
Foreach ( $Com in $a ) 
  {
     $command = {
     $PrinterPath = "\\domain\name"
     $net = New-Object -ComObject WScript.Network
     $net.AddWindowsPrinterConnection($PrinterPath)
     }
     Invoke-Command -ComputerName $com -Credential $Credential -ScriptBlock $command
  } 

Keep in mind that adding a printer in this fashion is specific to the current user. If you want to install the printer for all users you will want to go about creating a new TCP/IP port, and declaring the driver to use, etc.

I would do it as a login script. Then you wouldn’t need credentials. You can also use “add-printer -connectionname \server\printer”. This type of printer is a pita in my opinion, between things like client-side rendering not cleaning up sid’s of deleted profiles, and even not sometimes working the first time a user printer is created, and the little known fact of needing high random ports open on the print server to prevent a 45 second delay in adding the printer. Also, if you start a web browser before setting the default printer, it won’t show up in the web browser. If you could do lpr printers instead, I would recommend it.

I know this is a powershell site, but your easiest solution is to add the printer with group policy - quick and easy and installs when the user logs on.