Renaming computers remotely using PowerShell 2.0

Today a good friend asked to how to rename a computer remotely, in a domain,  using PowerShell 2.0. Well, if it was 3.0, the rename-computer cmdlet is the answer. But unfortunately in PowerShell 2.0 we need to use WMI and  a trick (it took me some time to figure out).

We will use the rename method from the Get-WmiObject and the class Win32_ComputerSystem, but the trick is you need to use the parameter –authentication PacketPrivacy and provide the username and password in the rename method.Otherwise the rename method will fail.

I wrote a function to do that, of course far way from the rename-computer in PowerShell 3.0, but that will do it.

The function has 3 parameters :

ComputerName – The old ComputerName

Newname – The new name

Credentials – User name and Password with rights to perform the operation


The function returns 3 properties :

HasSucceeded – True or False

OldComputername – The Old name

NewComputerName – New Name


Also it is an advanced function, and it have support to whatif and verbose parameters.

You just need to use :

Rename-Computer20 -Computername Test50 -Newname Test51–verbose

and it will star asking for the credentials (if you not already passed as parameter)


After you fill the credentials, the magic happens 🙂


As I said before, you also can use the whatif commom parameter  :


The code :

Function Rename-Computer20 {
try {

If (!($Psboundparameters.ContainsKey('Credentials'))) {
Write-Verbose 'Getting the User Name and Password'
$Credentials = Get-Credential -ErrorAction Stop

Write-Verbose "Testing the connection with computer $($Computername)"

try {
Test-Connection -ComputerName $Computername -ErrorAction Stop -Quiet |Out-Null

Write-Verbose "Checking if the new name $($Newname) exists !!!"

if (Test-connection -ComputerName $Newname -Quiet) {
Write-Error "Computer $($Newname) exists !!!"
} Else {

Write-Verbose "Test ok. Trying to rename Computer $($Computername) to $($Newname) "
$Hash = @{HasSucceeded = $False ; OldComputerName = "$($ComputerName)" ;NewComputerName = "$($NewName)"}

if ($PSCmdlet.ShouldProcess("Renaming Computer $($Computername) to $($Newname)")) {
if (!(Get-WmiObject Win32_ComputerSystem -ComputerName $Computername -Authentication PacketPrivacy -ErrorAction Stop).rename($Newname,[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($Credentials.Password)),$Credentials.UserName).ReturnValue) {
$Hash = @{HasSucceeded = $True ; OldComputerName = "$($ComputerName)" ;NewComputerName = "$($NewName)"}
Write-Warning "You must restart your computer for the changes to take effect"
Write-Output (New-Object PsObject -Property $hash)

} catch {
Write-Error "Connection with computer $($Computername) failed"
} catch {
Write-Error $error[0]

#Rename-Computer20 -Computername $args[1] -Newname $args[3] -whatif -verbose


Love PowerShell hã ?

About Laerte Junior

Laerte Junior Laerte Junior is a SQL Server specialist and an active member of WW SQL Server and the Windows PowerShell community. He also is a huge Star Wars fan (yes, he has the Darth Vader´s Helmet with the voice changer). He has a passion for DC comics and living the simple life. "May The Force be with all of us"
This entry was posted in Algo que Esqueci de Categorizar. Bookmark the permalink.

3 Responses to Renaming computers remotely using PowerShell 2.0

  1. Roman Mazi says:

    I find it very interesting in our environment (We are in not in Domain). Gwmi works fine. I receive error:
    VERBOSE: Performing operation “Rename-Computer20” on Target “Renaming Computer to vdtlj-wxp-test”.
    Rename-Computer20 : Connection with computer failed
    At line:1 char:18
    + Rename-Computer20 <<< gwmi -comp win32_operatingsystem -cred admindt|ft name

    Microsoft Windows XP Professional|C:\WINDOWS|\Device\Harddisk0\Partition1

    • Interesting. I didn’t tested in a non-domain environment and it looks like your OS is XP right ? I need to create a scenario to test it.
      Will do it and post here

      • Roman Mazi says:

        Yes OS is XP SP3. Thanks. I’ve never managed to rename computer with PS. However, wmic works:
        wmic.exe /node:”ip” /USER:admindistrator ComputerSystem Where Name=”oldname” Rename Name=”newname”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s