简体   繁体   中英

How to change network printer name using IP address in windows

Just wanted to post my solution here in case anyone needs it. We recently had to replace a printer that was widely used around the organization. We are using a universal driver for this printer and just needed to update the name of the printer on the machines. Here is the powershell script I came up with.

#Pulls printer name, but it is formatted and not clean. Replace IP with printer IP
$PrinterExtract = wmic printer where "PortName LIKE 'IP%'" GET Name 

#removes 'name' string from variable
$PrinterExtract = $PrinterExtract -replace 'Name',''

#removes all empty lines and formatting and creates a new variable
$PrinterName = ($PrinterExtract | Where { $_ -ne "" } | Out-String).Trim()

#pulls printer object
$PrinterObject = Get-Printer -Name $PrinterName

#renames printer. Replace new printer name with the name of new printer
Rename-Printer -InputObject $PrinterObject -NewName "New Printer Name"

I was trying to perform this task without using wmic but could not figure out a way to pull an existing printer's name from an IP. If anyone can provide a more modern solution, it would be appreciated.

Figured it out, if anyone needs it

    #Pulls printer name, but it is formatted and not clean
    $PrinterExtract = (Get-Printer | Where-Object {$_.PortName -Like "IP"} | Select Name | Where {$_ -ne ""} | Out-String).Trim()
    
    #removes 'name' string from variable
    $PrinterExtract = $PrinterExtract -replace 'Name',''
    
    #removes '----' string from variable
    $PrinterExtract = $PrinterExtract -replace '----',''
    
    #removes formatting and empty lines
    $PrinterName = ($PrinterExtract | Where { $_ -ne "" } | Out-String).Trim()
    
    #pulls printer object into new variable
    $PrinterObject = Get-Printer -Name $PrinterName
    
    #renames printer
    Rename-Printer -InputObject $PrinterObject -NewName "Printer Name"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM