Rename multiple computers listed in CSV file

Hi All, I’m getting an error and unable to resolve. CSV file as two (2) columns: oldname and newname.

$computers = import-csv -Path "c:\scripts\computers.csv" 
foreach ($oldname in $computers){
Rename-Computer -ComputerName $computers.oldname -NewName $computers.newname -DomainCredential Domain1\myname1 -force -restart
# restart-computer $computers.oldname -Force
# Test-Connection -BufferSize 32 -Count 1 -ComputerName PK-Training01 -Quiet

Error message:
Rename-Computer : Cannot convert ‘System.Object’ to the type ‘System.String’ required by parameter ‘ComputerName’. Specified method is not
At C:\Users\gkantorowitch\Desktop\PowerShell\Rename Computer multiple.ps1:4 char:31

Thank you all.

When you post error messages or console output please format it as code as well.

In your foreach() loop you create a loop variable $oldname but you’re using the input array variable $computers inside your loop.

I’d recommend using better variable names … someting like

$ComputerList = Import-Csv -Path 'c:\scripts\computers.csv' 
foreach ($Computer in $ComputerList){
    Rename-Computer -ComputerName $Computer.oldname -NewName $Computer.newname -DomainCredential Domain1\myname1 -Force -Restart

Hi Olaf, all,
This worked like a charm for me except that I have to enter the password for each loop. If I have ten (10) computer names in computers.csv I am being asked for the password ten (10) times.
Is there a way around this by tweaking this script??
Thank you.

outside of your loop (before) make a variable to capture your credentials.

$Credentials = Get-Credential Domain1\Username1  

Then on your Rename-Computer line provide $Credentials as the value to the -Credential parameter

I’d run the script with the proper account. Use the account having administrative rights on the target computers to run the script or the console you run this code snippet in. :man_shrugging:t3: