Posted On April 17, 2019

Script to Rename Computers in Active Directory

kimconnect 0 comments
blog.KimConnect.com >> Codes >> Script to Rename Computers in Active Directory

1. Run this PowerShell Script to rename 1 computer:

$admin = "KIMCONNECT\"+ Read-Host "Enter the admin username"
$pass = Read-Host 'Enter the admin password' -AsSecureString
$oldName = Read-Host 'Input Current Computer Name'
$newName = Read-Host 'Set $oldName As'

function renameComputer{
param (
[Parameter(Mandatory)][ValidateNotNullOrEmpty()][string[]]$old,
[Parameter(Mandatory)][ValidateNotNullOrEmpty()][string[]]$new,
[Parameter(Mandatory)][ValidateNotNullOrEmpty()][string[]]$adminID,
[Parameter(Mandatory)][ValidateNotNullOrEmpty()][string[]]$password
)

Write-Host "Renaming computer from: $old to: $new using $adminID with $password`nPress Ctrl + C to abort";
pause;
netdom renamecomputer $old /newName:$new /uD:$adminID /passwordD:$password /force /reboot
}
renameComputer -old $oldName -new $newName -adminID $admin -password $pass

2. To rename multiple computers:

a. Create a csv file with a set of computer names targeted for renaming:

SERVER001,AWS-SERVER001
SERVER007,AWS-SERVER007
SERVER898,AWS-SERVER898

b. Run this script

$adminID = "KIMCONNECT\"+ Read-Host "Enter the admin username"
$pass = Read-Host 'Enter the admin password' -AsSecureString

function renameComputers{
Write-Host "Importing Computers from CSV file"
$renameList = Import-Csv "C:\scripts\rename.csv" -Header @("old","new")
foreach ($item in $renameList) {

# Test the input string to skip null values
if !([string]::IsNullOrEmpty($item.old) -or [string]::IsNullOrEmpty($item.new)){
$oldName = $item.old;
$newName = $item.new;
Write-Host "Renaming computer from: $oldName to: $newName";
netdom renamecomputer $oldName /newName:$newName /uD:$adminID /passwordD:$pass /force /reboot
} #endif
}
renameComputers

Leave a Reply

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

Related Post

Indications that Chocolatey is locked down

# System's version is less than vendor's current (Chocolatey v0.10.15) 0+000+00[LAX-WEB005]: PS C:\Users\testadmin\Documents> choco source…

PowerShell: Run PowerShell As Another User

# Set credential $adminUsername='domain\adminDude' $adminPassword='whatpassword?' $GLOBAL:adminCredential=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $adminUsername,$(ConvertTo-securestring $adminPassword -AsPlainText -Force) # Test…

Office 365 Active Directory Hybrid Accounts Administration

Intro This scenario assumes that Azure AD Sync has been used to synchronize on premise…