CBC Computer Services

PowerShell Script to Change Computer’s Name

Here’s a PowerShell script to change a computer’s name:

# Get the current computer name
$OldName = $env:COMPUTERNAME
Write-Host "Current computer name: $OldName"

# Get the new computer name from the user
$NewName = Read-Host "Enter the new computer name"

# Confirm the change
$Confirmation = Read-Host "Are you sure you want to rename the computer to '$NewName'? Type 'Y' to confirm."

if ($Confirmation -eq "Y") {
    try {
        # Rename the computer
        Rename-Computer -NewName $NewName -Force

        Write-Host "Computer has been renamed to '$NewName'. A restart is required to apply the changes."

        # Ask if the user wants to restart now
        $RestartConfirmation = Read-Host "Would you like to restart now? Type 'Y' to confirm."

        if ($RestartConfirmation -eq "Y") {
            Restart-Computer
        } else {
            Write-Host "You can restart the computer later to apply the changes."
        }
    } catch {
        Write-Host "Failed to rename the computer: $_"
    }
} else {
    Write-Host "Operation canceled."
}

Steps to run the script:

  1. Open PowerShell as an administrator.
  2. Paste the script into PowerShell or save it as a .ps1 file and run it.
  3. Enter the new computer name when prompted and confirm the changes.
  4. Optionally, restart the computer to apply the name change.

This script prompts the user for the new computer name, renames the system, and offers to restart the computer immediately.