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: Checking Computer Type

# Determine what type of Windows machine (client or server)switch ((Get-CimInstance -ClassName Win32_OperatingSystem).ProductType){1 {'client'} #…

Some Common Gitlab Commands

To ignore SSL Cert errors: git config --global http.sslVerify "false"   # checkout changed file…

PowerShell: Validate SQL Server Credentials

Add this to your SQL toolbox so that it'll be quick and easy to validate…