Thursday, February 16, 2012

Set Computer Name using PowerShell.


Hi,

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.

ComputerName
Download the script from Here : http://gallery.technet.microsoft.com/scriptcenter/Set-Computername-using-9cb6ecc4

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.

Capture-2 

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

Capture-3

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

Capture-4

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"

Capture-5

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

Capture-6

Capture-7

Now check if you computer name get changed or not.

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

Capture-8

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://gallery.technet.microsoft.com/scriptcenter/Set-Computername-using-9cb6ecc4

Thanks for reading .
Aman Dhally
aafa005

4 comments:

  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.

    ReplyDelete
    Replies
    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 :)

      thanks
      aman

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

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


    Summer

    ReplyDelete

Note: Only a member of this blog may post a comment.