1

I want to change the name of a windows 7 machine from input from a user using a BAT file. The name i want to change is the name of the computer in advanced system settings. (see screen shot below)

enter image description here

The below code is what i have tried, but does not work. I have also tried running the code as administrator, and then restarting, did not work either.

SET /P PCNAME=Please enter your name: 
REG ADD HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName /v ComputerName /t REG_SZ /d %PCNAME% /f
BLang
  • 930
  • 3
  • 16
  • 35
  • his issue was not the same, he was deleting his input so it was never getting stored. I do not have that. – BLang Oct 28 '16 at 18:27
  • You can do it easily with a vbscript ! – Hackoo Oct 28 '16 at 21:08
  • 3
    Why don't you search stackoverflow first place? It was no effort to get to [link](https://stackoverflow.com/questions/54989/change-windows-hostname-from-command-line)] with some solutions. –  Oct 28 '16 at 22:03
  • Thanks, sorry about that!! Have been traveling lots for work and this was a problem I'd left off with! – BLang Nov 01 '16 at 01:39

1 Answers1

5

A quick google brings this command :

WMIC ComputerSystem where Name=COMPUTERNAME call Rename Name=NewName

If the computer name has dashes or other special characters you need to quote the computer name

WMIC ComputerSystem where Name="COMPUTER-NAME" call Rename Name=NewName

Source

With powershell method :

Powershell 3.0 (Windows 8) introduced the Rename-Computer cmdlet. Example:

Rename-Computer -NewName NewComputerName -Restart

This will rename the computer and immediately restart.

TechNet Documentation.

With Vbscript Method :

Option Explicit
Title = "Renaming PC"
Dim Title,strComputer,objWMIService,strNewName,objComputer
Dim Obj,Question,err,strDescription,colComputers,x
'Run as Admin
If Not WScript.Arguments.Named.Exists("elevate") Then
   CreateObject("Shell.Application").ShellExecute DblQuote(WScript.FullName) _
   , DblQuote(WScript.ScriptFullName) & " /elevate", "", "runas", 1
    WScript.Quit
End If
'************************************Main Script****************************************
Call Rename_PC()
'If you want to change the description of the computer, you should uncomment this line :
'Call Changing_Descrption()
Call Ask4Reboot()
'*********************************Changing PC Name *************************************
Sub Rename_PC()
strComputer = "." 
Set objWMIService = GetObject("winmgmts:" _ 
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
strNewName = Inputbox ("Enter the new name of the PC : ",Title,"Salle-Poste")
If strNewName = "" Then Wscript.Quit()
Set colComputers = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
For Each objComputer in colComputers
    err = objComputer.Rename(strNewName)
Next
End Sub
'*************************** Changing the description **********************************
Sub Changing_Descrption()
strDescription = Inputbox("Enter Description : ",Title,"Machine blalllaaaaaaa")
If strDescription = "" Then Wscript.Quit()
Set Obj= GetObject("winmgmts:\\" & strComputer).InstancesOf("Win32_OperatingSystem")
For Each x In Obj 
   x.Description = strDescription
   x.Put_
Next
End Sub
'***************************************************************************************
Sub Ask4Reboot()
Question = MsgBox("PC name will change " & DblQuote(strNewName) & " after restarting this computer" & vbCrLf &_
"Yes to restart" & vbCrLF &_
"No to cancel the restart" & vbtab & "?",VbYesNo+VbQuestion,Title)
If Question = VbYes then 
    Reboot()
Else
    wscript.Quit(1)
End If
End Sub
'**************************************
Function DblQuote(Str)
    DblQuote = chr(34) & Str & chr(34)
End function
'**************************************
Sub Reboot()
Dim ws,Command,Result
Set ws = CreateObject("Wscript.Shell")
Command = "Shutdown.exe /r /t 20 /c "& DblQuote("Save your documents - PC restarts in 20 seconds")
Result = ws.run(Command,0,True)
End Sub
'**************************************
Hackoo
  • 18,337
  • 3
  • 40
  • 70