Rename Domain PC: A PowerShell Script

This blog post will walk you through a PowerShell script that allows you to rename a domain PC. The script is designed to interact with the user through a graphical interface, making it easy to use and understand.

Prerequisites


The Script

The script consists of three main sections: importing assemblies, generating form objects, and implementing event script blocks. We'll break down each section below.


#Generated Form Function
function GenerateForm {
########################################################################
# Generated By: reaston
########################################################################

#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
#endregion

#region Generated Form Objects
$form1 = New-Object System.Windows.Forms.Form
$results = New-Object System.Windows.Forms.ListBox
$btn_Rename = New-Object System.Windows.Forms.Button
$NewName = New-Object System.Windows.Forms.TextBox
$label2 = New-Object System.Windows.Forms.Label
$Old_Name = New-Object System.Windows.Forms.TextBox
$TPN = New-Object System.Windows.Forms.Label
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects

#----------------------------------------------
#Generated Event Script Blocks
#----------------------------------------------
#Provide Custom Code for events specified in PrimalForms.
$btn_Rename_OnClick= 
{
#TODO: Place custom script here
#Get Admin Credentials
Function RenamePC {
    $Global:Credential = Get-Credential "$env:UserName"
}
    if ($Old_Name.Text -eq "" -or $New_Name.text -eq "") {
    $Results.Items.clear()
    $Results.ForeColor ='orange'
    $Results.items.add("=======================================")
    $Results.Items.add("Please check your entries and try again")
    $Results.items.add("=======================================")
} else {
    $Results.Items.clear()
    $Results.ForeColor ='blue'
    $Old_Name = $Old_Name.Text
    $NewName = $NewName.Text
    if (test-Connection -ComputerName $Old_Name -Count 2 -Quiet ) {  
        $Results.ForeColor ='green'
        $results.items.add("$Old_Name is online")
        $results.Items.Add("Checking New Name for conflicts")
        $tempVar = Get-ADComputer $NewName
        if ($null -eq $tempVar) {
            $Results.ForeColor ='green'
            $results.items.add("$NewName is free :)")
            Get-WMIObject Win32_BIOS -computername $Old_Name
            # rename and restart target
            Rename-Computer -NewName $NewName -ComputerName $Old_Name -DomainCredential $env:UserName -Restart
            $results.items.add("New PCname is $NewName")
        } else {
            $Results.ForeColor ='Orange'
            $results.items.add("$NewName already exists on AD :(")
        }
    } else {
        $Results.Items.clear()
        $Results.ForeColor ='red'
        $results.items.add("$Old_Name did not respond to pings")
    }  
}
}

$OnLoadForm_StateCorrection={
#Correct the initial state of the form to prevent the .Net maximized form issue
	$form1.WindowState = $InitialFormWindowState


}

#----------------------------------------------
#region Generated Form Code
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 261
$System_Drawing_Size.Width = 287
$form1.ClientSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.Location.X = 100
$form1.Location.Y = 200
$form1.Name = "form1"
$form1.Text = "Rename Domain PC"

$Results.BackColor = [System.Drawing.Color]::FromArgb(255,0,0,0)
$Results.DataBindings.DefaultDataSourceUpdateMode = 0
$Results.ForeColor = [System.Drawing.Color]::FromArgb(255,51,153,255)
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 18
$System_Drawing_Point.Y = 101
$Results.Location = $System_Drawing_Point
$Results.Name = "Results"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 152
$System_Drawing_Size.Width = 252
$Results.Size = $System_Drawing_Size
$Results.TabIndex = 6
$Results.Text = "Messages will be displayed here."

$form1.Controls.Add($Results)

$btn_Rename.BackColor = [System.Drawing.Color]::FromArgb(255,50,205,50)

$btn_Rename.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 205
$System_Drawing_Point.Y = 63
$btn_Rename.Location = $System_Drawing_Point
$btn_Rename.Name = "btn_Rename"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 28
$System_Drawing_Size.Width = 66
$btn_Rename.Size = $System_Drawing_Size
$btn_Rename.TabIndex = 5
$btn_Rename.Text = "Rename"
$btn_Rename.UseVisualStyleBackColor = $False
$btn_Rename.add_Click($btn_Rename_OnClick)

$form1.Controls.Add($btn_Rename)

$NewName.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 108
$System_Drawing_Point.Y = 37
$NewName.Location = $System_Drawing_Point
$NewName.Name = "NewName"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 164
$NewName.Size = $System_Drawing_Size
$NewName.TabIndex = 3

$form1.Controls.Add($NewName)

$label2.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 13
$System_Drawing_Point.Y = 40
$label2.Location = $System_Drawing_Point
$label2.Name = "label2"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 88
$label2.Size = $System_Drawing_Size
$label2.TabIndex = 2
$label2.Text = "New PC Name"

$form1.Controls.Add($label2)

$Old_Name.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 108
$System_Drawing_Point.Y = 13
$Old_Name.Location = $System_Drawing_Point
$Old_Name.Name = "Old_Name"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 164
$Old_Name.Size = $System_Drawing_Size
$Old_Name.TabIndex = 1

$form1.Controls.Add($Old_Name)

$TPN.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 13
$System_Drawing_Point.Y = 16
$TPN.Location = $System_Drawing_Point
$TPN.Name = "TPN"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 19
$System_Drawing_Size.Width = 98
$TPN.Size = $System_Drawing_Size
$TPN.TabIndex = 0
$TPN.Text = "Target PC Name"

$form1.Controls.Add($TPN)

#endregion Generated Form Code

#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$form1.ShowDialog()| Out-Null
}