Set Computer Name using PowerShell
Yesterday I faced a problem in which I needed to change the computer name using PowerShell. First I was looking for some cmdlet which can do this job for me but there is not any inbuilt cmdlet for it.
Then I searched and finally find a way by which I can do this within the PowerShell. This method is using WMI to do the task.
http://lh3.ggpht.com/-sjVc2WKl7ig/TzyiZWmpu1I/AAAAAAAACTY/I08UeuABm2k/Capture_thumb%25255B3%25255D.png?imgmax=800
Get-WmiObject Win32_ComputerSystem
We are using Get-WMI to give us all the value of Win32_ComputerSystem.
You can see in the Output that the "Name value contain our Computer name "WINANAL-088y8gx" with mach with our above screenshot.
http://lh4.ggpht.com/-ZVqLqOETDuQ/Tzyib5sQ5RI/AAAAAAAACTo/e362iLXcvBQ/Capture-2_thumb%25255B1%25255D.png?imgmax=800
$computerName = Get-WmiObject Win32_ComputerSystem
We are putting the command in to the $computerName variable. And you can see that the output of $computerName is the same and above screenshot in which we run the command alone.
http://lh5.ggpht.com/-lLHzSnNy6G4/TzyiebS7W3I/AAAAAAAACT4/W4rWLD2xZNM/Capture-3_thumb%25255B1%25255D.png?imgmax=800
$name = Read-Host -Prompt "Please Enter the ComputerName you want to use."
In $name variables we are using **Read-Host **cmdlet with -Prompt argument with asking users to provide a computer name to be use.
http://lh5.ggpht.com/-KbAvAcxIBf8/Tzyigja2wDI/AAAAAAAACUI/CVbmrjSYy6Y/Capture-4_thumb%25255B1%25255D.png?imgmax=800
Whatever user provide to Read-Host, the value is stored in the $name variable. In First example in below screenshot we didn't provide anything to Read-Host and you can see that output of $name is blank. In next example we provide "Test-Laptop" to Read-Host and you can see that now $name contains the value of "Test-Laptop"
http://lh6.ggpht.com/-QeFnZUgXqag/TzyijtILq_I/AAAAAAAACUY/DTAwDhFQTJ8/Capture-5_thumb%25255B1%25255D.png?imgmax=800
$computername.Rename($name)
The Win32_ComputerSystem WMI class contain the method of .Rename() in which we need to provide a desired computer name in brackets.
http://lh3.ggpht.com/-3W07HmPmQ2k/Tzyil_M_OqI/AAAAAAAACUo/7KTAMn0OMnY/Capture-6_thumb%25255B1%25255D.png?imgmax=800
http://lh3.ggpht.com/-Dhv8dzykHqk/TzyioakdU7I/AAAAAAAACU4/xfEky7nvOEk/Capture-7_thumb%25255B1%25255D.png?imgmax=800
Now check if you computer name get changed or not.
Bingo !!! Yes, it is changed.. now reboot your laptop ;) using **restart-Computer **
http://lh6.ggpht.com/-tm9x7F0GCjA/Tzyiq1raGrI/AAAAAAAACVI/LB9EKbmC47w/Capture-8_thumb%25255B1%25255D.png?imgmax=800
And if you want a GUI box to to open a Pop-UP to insert computer Name try this
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
$name = [Microsoft.VisualBasic.Interaction]::InputBox("Enter Desired Computer Name ")
$name
Download the script from Here : http://dl.dropbox.com/u/17858935/Change_ComputerName.zip
Thanks for reading.
Aman Dhally