Powershell - Renaming Computer Name

karthik palani 1,036 Reputation points
2023-06-06T05:45:55.9933333+00:00

Hi All,

We have almost 10K+ machines, in which we wanted to change the computer name remotely using Powershell script.

We have a CSV file in which old name and new name should be replaced automatically. I tried below but its not working,

https://stackoverflow.com/questions/73847619/renaming-computers-with-a-csv-file

https://social.technet.microsoft.com/wiki/contents/articles/2243.how-to-rename-computers-using-powershell-and-a-csv-file.aspx

Kindly suggest the working script

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. MotoX80 36,291 Reputation points
    2023-06-06T11:58:55.83+00:00

    I tried below but its not working,

    What error did you get?

    Try example 2 on this page to rename a single computer.

    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/rename-computer?view=powershell-5.1

    For the -DomainCredential parameter, I would expect that you will need to pass a credential object. Something like this.

     $User = "NyDomain\Myadmin"
     $PWord = ConvertTo-SecureString -String "myadminpassword" -AsPlainText -Force
     $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord
     Rename-Computer -ComputerName Test1 -NewName Test001 -DomainCredential $Credential -force  
    

  2. MotoX80 36,291 Reputation points
    2023-06-07T12:19:39.8+00:00

    Test this with a few entries in the csv before you run it against all 10k.

    $AllComputers = import-csv -Path C:\temp\Computers.csv
    $User = "NyDomain\Myadmin"
    $PWord = ConvertTo-SecureString -String "myadminpassword" -AsPlainText -Force
    $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord 
    foreach ($Computer in $AllComputers) {
         Rename-Computer -ComputerName $Computer.oldname -NewName $Computer.newname -DomainCredential $Credential -force 
    }
    
    

    If your PC's respond to a ping, then you might want to use a version that has more error handling. Some of your pc's will be shut off, so you will likely need to run the script multiple times. You could also regenerate the csv with names that got missed.

    $AllComputers = import-csv -Path C:\temp\Computers.csv
    $User = "NyDomain\Myadmin"
    $PWord = ConvertTo-SecureString -String "myadminpassword" -AsPlainText -Force
    $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord 
    foreach ($Computer in $AllComputers) {
        $ping =  Test-NetConnection -ComputerName $computer.oldname  -ErrorAction Stop -WarningAction SilentlyContinue
        if ($ping.PingSucceeded) {
            try { 
                Rename-Computer -ComputerName $Computer.oldname -NewName $Computer.newname -DomainCredential $Credential -force -ErrorAction Stop 
                 "{0} successfully renamed to {1}" -f $Computer.oldname,  $Computer.newname 
            } catch {
                 "Unable to rename {0}, Error={1}" -f $Computer.oldname,$_
            }
        } else {
            "Unable to ping {0}" -f $Computer.oldname
        }
    }
    
    0 comments No comments

  3. karthik palani 1,036 Reputation points
    2023-06-15T06:14:05.3166667+00:00

    HI,

    I tried both the commands, it throws an error as below,

    Could you please advice

    PS1.jpg


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.