'Renaming Multiple Printers "Rename-Printer"
Having trouble with trying to import a .csv or .txt with multiple printer names (shown below). Running this locally on a server to change printer names in a list.
oldName,newName
printer1,printer2
From what VSCode is telling me is that these lines are erroring out for some reason and I can not figure it out for the life of me. Commented out some lines for testing.
Rename-Printer -Name $OldName -NewName $NewName
#Rename-Printer -InputObject $Computer -NewName $Computers.newName -Verbose
Code below (was trying .txt for a bit) :
#$Computers = import-csv .\printers.csv -Header 'oldName', 'newName'
$Computers = Import-Csv .\printers.txt -Header 'oldName', 'newName'
foreach ($Computer in $Computers){
$OldName = $($Computer.oldName)
$NewName = $($Computer.newName)
Rename-Printer -Name $OldName -NewName $NewName
#Rename-Printer -InputObject $Computer -NewName $Computers.newName -Verbose
}
Get-Printer | select Name,Comment,ShareName,PortName | Export-Csv C:\Users\$username$\Desktop\exportPrinters.csv -NoTypeInformation
Pause
Solution 1:[1]
You don't need that much code for such a simple task
Import-Csv 'C:\temp\test.csv' | %{Rename-Printer -Name $_.oldName.Trim() -NewName $_.newName.Trim() -ErrorAction Continue}
Get-Printer | select Name,Comment,ShareName,PortName | Export-Csv $env:USERPROFILE\Desktop\exportPrinters.csv -NoTypeInformation
Pause
Issues were: "Import-csv" will automatically pick up headers, and your "Export-Csv" had an extra $ in it. If you're concerned about whitespace, adding .Trim() to a string object will trim any whitespace from the ends.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
