Add-PrinterPort / Set-Printer Issue

I’m trying to update the printer port for multiple printers on multiple servers in a cluster. The cluster is for specific software which sends print jobs to its own printers. Each of the printers are configured using local ports that point to the ‘real’ print server. The local port looks like “\old\printer” for each of the printers.

I’d like to update them in PowerShell but I keep running into issues.

Currently I’m trying to add a new printer port to each of the printers like this.

$Printers = (get-printer).where({$_.PortName -like "\\OLD\*"})
foreach ($printer in $Printers){
	add-printerport( $Printer.PortName.Replace("\\OLD\","\\NEW\")) -Name $($Printer.Name)

The replace string replaces the old print server name with the new print server name that I want to update these printers with.

I get this error for each printer.

Add-PrinterPort : Parameter set cannot be resolved using the specified named parameters.
At line:2 char:1
+ add-printerport( $Printer.PortName.Replace("\\OLD\","\\NEW ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Add-PrinterPort], ParameterBindingException
    + FullyQualifiedErrorId : AmbiguousParameterSet,Add-PrinterPort

I’ve also tried.

foreach ($Printer in $Printers) {
	Set-Printer -Name $Printer.Name -PortName $($Printer.PortName.Replace("\\OLD\","\\NEW\"))

Which appeared to do nothing…

What am I doing wrong?

I was able to resolve the issues I was having.

To add a new printer port it needed to be done independently of the name to use. Whereas I was using

Add-PrinterPort -Name $($Printer.PortName.Replace("\\printman\","\\cacitspdquepr1\"))

What I needed to use was

$NewPort = $Printer.PortName.Replace("\\printman\","\\cacitspdquepr1\")
Add-PrinterPort -Name $NewPort

Similarily with Set-Printer, where I was using

Set-Printer -Name $Printer.Name -PortName $($Printer.PortName.Replace("\\OLD\","\\NEW\"))

It needed to be

$ThisPrinter = Get-Printer -Name $Printer.Name
Set-Printer -Name $ThisPrinter.Name -PortName $Printer.PortName.Replace("\\OLD\","\\NEW\")
1 Like

I have no printers to test with sorry, but this error tells me it doesn’t know what parameter set to use based on the provided parameters. I’d say it’s typically best practice to specify a parameter name for each and not rely on positional parameters, as it makes it clearer when you are writing what parameters you are passing.

Also you may want to set the name in a var prior to actually doing it in the command

$var = $Printer.PortName.Replace('blah', 'blah')

Add-PrinterPort (PrintManagement) | Microsoft Learn

Take a look at each section under syntax. those are your parameter sets. It might be that you’re trying to set the name up in the command and then set it to the var in the second part, so not 100% sure what you’re attempting there

Ah, ok. yep this was sorta what I was getting at, but you responded to post seconds before I posted heh.