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

PowerShell: Remove Hyper-V VM Snapshots

Hyper-V's snapshot feature is a double-edged sword. It's a convenient method to make a quick…

PowerShell: Discover Environment

One of the most common tasks of Active Directory cleanup is to perform a discovery…

Check Servers NSLookup of a Listener to Match Active Node IP

$servers="SQL01","SQL02","SQL03","SQL04" $listener="halistener01" $activeNode="10.10.10.5" # Dynamic Credential method 1 $who = whoami if ($who.Substring($who.length-2, 2)="-admin"){$username=$who;} else…
Other project: DragonCoin.com