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 Enable GPUPDATE When Connected via OpenVPN Client

// Explanation: to prevent locking of processes, we set a proxy batch file (on_connect.bat) to…

How To Remove A Program on Windows Using PowerShell

# removeAppwizProgram.ps1 # Version 0.02 $computernames=@( 'SERVER0001', 'SERVER0002' ) $appName='Dell EMC OpenManage Systems Management Software…

PowerShell: Install PSTools

if(!(get-command psexec -ea SilentlyContinue)){ if (!(Get-Command choco.exe -ErrorAction SilentlyContinue)) { Set-ExecutionPolicy Bypass -Scope Process -Force;…