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

How To Use Command Line to Configure iDrac Settings

Step 1: Install RacADM $computerlist=@' SERVER1 SERVER2 '@ $computernames=@($computerlist -split "`n")|%{$_.Trim()} $fileURL="https://dl.dell.com/FOLDER08543783M/1/DellEMC-iDRACTools-Web-WINX64-10.3.0.0-4945.exe" $expectedExecutable='racadm.exe' $expectedInstallPath='C:\Program Files\Dell\SysMgt\iDRACTools\racadm'…

PowerShell: Check if a HostName is Resolvable on All Internal DNS Servers

# Check if servername is resolvable at all DCs$serverName="MIGRATED-SHERVER" function checkDns{ param( $serverName, $dnsServers=(Get-ADDomainController -Filter…

PowerShell: List Biggest Files and Folders on a Volume

# listBigFilesAndFolders-v0.01.ps1 # Set variables $parentDirectory="C:\" $depth=4 $topX=10 $excludeDirectories="C:\Windows","C:\Program Files","C:\Program Files (x86)" $clusterSizeOfNetworkShare=8192 # Sanitize…
Other project: DragonCoin.com