更改Windows计算机名称BAT文件

时间:2016-10-28 18:19:16

标签: batch-file

我想使用BAT文件从用户输入更改Windows 7计算机的名称。我想要更改的名称是高级系统设置中的计算机名称。 (见下面的屏幕截图)

enter image description here

以下代码是我尝试过的,但不起作用。我也尝试以管理员身份运行代码,然后重新启动,也没有工作。

verify=False

1 个答案:

答案 0 :(得分:3)

快速谷歌带来了这个命令:

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

如果计算机名称包含破折号或其他特殊字符,则需要引用计算机名称

Rename-Computer

Source

使用PowerShell方法:

Powershell 3.0(Windows 8)引入了Rename-Computer -NewName NewComputerName -Restart cmdlet。例如:

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
'**************************************

这将重命名计算机并立即重新启动。

TechNet Documentation

使用Vbscript方法:

/(?:https?:\/\/)?(?:www\.)?(mbasic.facebook|m\.facebook|facebook|fb)\.(com|me)\/(?:(?:\w\.)*#!\/)?(?:pages\/)?(?:[\w\-\.]*\/)*([\w\-\.]*)/ig