Monday, 18 March 2013

Rename Windows 7/2008 in Powershell

I recently had a requirement to deliver a Windows VM which would be deployed by the end user and needed a unique name.
I also needed the computer name to match our naming convention - in this case it had to begin with the letters zzz.
The script below runs in powershell and prompts for the new computer name, prepends the word "zzz" and then renames and reboots the machine.
I put this command in a scheduled task that runs on login.




$ComputerInfo = Get-WmiObject -Class Win32_ComputerSystem
$ComputerName = gc env:computername
If ($ComputerName -notmatch "ZZZ") {
write-host -foregroundcolor Red "Existing Computer Name: $ComputerName"
write-host -foregroundcolor Red "You need to change the name so it doesn't clash with others"
$NewName = read-host -prompt "New name (ZZZ will be prepended automatically)"
$ComputerInfo.Rename("slops"+$NewName)
Restart-Computer
}