best business builder

Remotely Rename Computer’s Hostnames

Below is a PowerShell script that will change the Hostname of a domain computer remotely.

You will need to build a .csv file of computer’s Hostnames you wish to change with two columns: OldName and NewName
NOTE: When making the .csv make sure you Save As a .csv. DO NOT create a .txt and change the extension from .txt to .csv. This will not work.

##############################################################################################################
########## Enter the full path to your computer file below. The excel file must be saved as .csv format ######
########## Don't change the headers, they need to be OldName and NewName #####################################
$ComputerFile = Import-Csv C:\users\username\Documents\computerfile.csv -Header OldName, NewName

########## Enter your admin username below. Do not delete the quotation marks ################################
$Username = "domainadminaccountname"



$GetPass = Read-Host -Prompt "Enter the password for $($Username)" -AsSecureString
$Uname = "domain\$($Username)"
$RenameCred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Uname,$GetPass

foreach ($comp in $ComputerFile){ 
write-output "Attempting change of $($comp.OldName) to $($comp.NewName)"
write-output "Attempting change of $($comp.OldName) to $($comp.NewName)" | Out-File C:\users\$username\Documents\script.log -Append
#Verify if the name has been changed and is reachable
    if (Test-Connection $comp.NewName -ErrorAction SilentlyContinue -WarningAction SilentlyContinue -Quiet){
        $NewComputerName = Invoke-Command -comp $comp.NewName -script {Get-WmiObject -Class Win32_BIOS | Select -Property PSComputerName} 
        if ($NewComputerName.PSComputerName -eq $comp.newname){
            Write-Output "$($comp.OldName) has already been renamed to $($comp.NewName)" | Out-File C:\users\$username\Documents\script.log -Append
        }
    }
#Verify if the system is reachable using its old name
    elseif(Test-Connection $comp.OldName -ErrorAction SilentlyContinue -WarningAction SilentlyContinue -Quiet){
        $OldComputerName = Invoke-Command -comp $comp.OldName -script {Get-WmiObject -Class Win32_BIOS | Select -Property PSComputerName} 
        if ($OldComputerName.PSComputerName -eq $comp.OldName){
            Write-Output "$($Comp.OldName) found, renaming to $($Comp.NewName)" | Out-File C:\users\$username\Documents\script.log -Append
            Rename-Computer -ComputerName $comp.OldName -NewName $comp.NewName -DomainCredential $RenameCred -Force 
            if ($?){ 
                Write-Output "$($comp.OldName) has been successfully renamed to $($comp.NewName) but may require a restart" | Out-File C:\users\$username\Documents\script.log -Append
            }
            else{
                Write-Output "Something whent wrong while trying to rename $($comp.OldName) to $($comp.NewName)" | Out-File C:\users\$username\Documents\script.log -Append
            }
        }
    else{ Write-Output "The System could not be reached using $($comp.OldName) or $($comp.NewName), please verify the system is online and reachable." | Out-File C:\users\$username\Documents\script.log -Append}
    }
}
customer relationships

Leave a Reply

Your email address will not be published. Required fields are marked *