Powershell function to rename a computer

This is something I took from one of my larger deployment scripts and found it to be very useful and reusable.
Just to give you a use case: In my situation the parameter $NewComputerName is calculated based on the device type (server, laptop or desktop), the IP range and a random number taken from a text file located on a file share.

I hope you find it useful Glimlach

Function Rename-Computer ($NewComputerName)
{
# Get the current credential.
$credential = Get-Credential

  # Get the current computer name
$OldComputerName = $Env:ComputerName

# Change the computer name.
Get-WmiObject Win32_ComputerSystem -ComputerName $OldComputerName -Authentication 6 | ForEach-Object {$_.Rename($NewComputerName,$credential.GetNetworkCredential().Password,$credential.Username)}

  # Reboot the computer to let the change take affect.
Shutdown.exe -r –t 7
}

2 comments

  1. Andrew Barnes. says:

    Nice post. I’d keep it 100% powershell by using the Restart-Computer cmdlet. http://technet.microsoft.com/en-us/library/hh849837.aspx

  2. Jeff Wouters says:

    Hi Andrew,
    Thanks. I agree, native PoSH is better. The post was written before PS 3 was RTM, that’s why I didn’t. But good of you to mention it, thanks!
    Jeff

Leave a Reply

Your email address will not be published. Required fields are marked *