Thursday, February 16, 2012

Set Computer Name using PowerShell.


Yesterday I faced a problem in which I need 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.

Download the script from Here :

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.


$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 same and above screenshot in which we run the command alone.


$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.


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"



The Win32_ComputerSystem WMI class contain the method of .Rename() in which we need to provide a desired computer name in brackets.



Now check if you computer name get changed or not.

Bingo !!! Yes It is changed..  now reboot you laptop ;) using  restart-Computer


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 ")

Download the script from Here :

Thanks for reading .
Aman Dhally


  1. What is
    write-host $([char]7)
    supposed to do?
    I don't see any output from it except blank lines. I'm just curious.

    I don't understand $([char]7), because I thought [char]7 just cast the 7 as a char.

    1. Hi,

      $([char]7) is a special character with do Beep.. Trun on your laptop speakers and then type and enter $([char]7) , you will hear a beep sound :)


  2. It looks like [char]7 is from an ASCII Table, but what symbol is it? Is it a blank?

  3. I did it! I just follow the instruction you said, and viola I already set my computer name. Thanks for sharing.


  4. Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing.Nice article i was really impressed by seeing this article, it was very interesting and it is very useful for me.
    Home Tutors in Delhi | Home Tutors Delhi

  5. Some tips on how to write essay about vocational schools and even maybe attend one. It could be interesting