从命令行更改Windows主机名

34

是否可以使用Windows 2003自带工具在命令行中更改主机名?


4
175K次观看……5个月前活跃……12年前提出并于2年前关闭。难道我们不能让有用的帖子继续存在吗?虽然这个问题可能不是很好,但它确实符合主题。 - FreeSoftwareServers
@dmo 这个脚本可能会很有趣:https://gist.github.com/timnew/2373475 - SebMa
8个回答

53

之前提到的wmic命令是一个很好的选择,因为它在最近版本的Windows中默认安装。以下是我的小改进,通过从环境中检索当前名称来实现通用化:

wmic computersystem where name="%COMPUTERNAME%" 
     call rename name="NEW-NAME"

注意:该命令必须在一行中输入,但我将其分成两行以避免出现滚动条。正如@rbeede所提到的,您需要重新启动以完成更新。


6
执行后需要重新启动。 - rbeede
我认为wmic不随Windows XP一起提供。 - IdontCareAboutReputationPoints
稍微简短一点:wmic computersystem where name="%COMPUTERNAME%" rename "NEW-NAME" - MOPO3OB

14

命令(cmd):

netdom renamecomputer %COMPUTERNAME% /Newname "NEW-NAME"

PowerShell(Windows 2008/2012):

netdom renamecomputer "$env:COMPUTERNAME" /Newname "NEW-NAME"

之后,您需要重新启动计算机。


1
至少在Win8上不行。netdom未被识别。 - NoBugs
works fine in win 2016 core - Mark Jones
在Win 10上无法工作,但在Win 2016服务器上可以工作。 - Bennett Yeo

1
使用以下命令远程更改计算机主机名,更改后需要重新启动系统。

psexec.exe -h -e \\\IPADDRESS -u USERNAME -p PASSWORD netdom renamecomputer CurrentComputerName /newname:NewComputerName /force


1
为什么要简单,当可以复杂?为什么要使用像netdom.exe这样的第三方应用程序,而不是正确地进行查询?尝试两个查询:
wmic computersystem where caption='%computername%' get caption, UserName, Domain /format:value wmic computersystem where "caption like '%%%computername%%%'" get caption, UserName, Domain /format:value 或者在批处理文件中使用循环:
for /f "tokens=2 delims==" %%i in ('wmic computersystem where "Caption like '%%%currentname%%%'" get UserName /format:value') do (echo. UserName- %%i)

1

我不知道有没有一个命令可以做到这一点,但你可以用VBScript或类似的东西来实现。像这样:

sNewName = "put new name here" 

Set oShell = CreateObject ("WSCript.shell" ) 

sCCS = "HKLM\SYSTEM\CurrentControlSet\" 
sTcpipParamsRegPath = sCCS & "Services\Tcpip\Parameters\" 
sCompNameRegPath = sCCS & "Control\ComputerName\" 

With oShell 
.RegDelete sTcpipParamsRegPath & "Hostname" 
.RegDelete sTcpipParamsRegPath & "NV Hostname" 

.RegWrite sCompNameRegPath & "ComputerName\ComputerName", sNewName 
.RegWrite sCompNameRegPath & "ActiveComputerName\ComputerName", sNewName 
.RegWrite sTcpipParamsRegPath & "Hostname", sNewName 
.RegWrite sTcpipParamsRegPath & "NV Hostname", sNewName 
End With ' oShell 

MsgBox "Computer name changed, please reboot your computer" 

原始


1

可以使用netdom.exe命令行程序。该程序可从Windows XP支持工具或Server 2003支持工具(均在安装CD上)中获取。

使用指南此处


3
如果您能描述如何使用它(就像@litao的回答中所述),那将更好,因为您提供的链接现在已经失效了。 - Matthieu
1
FYI,11年过去了,链接已经失效。这就是为什么添加示例很有用。 - Mike Mackintosh
指南仍然可在此处找到(https://www.betaarchive.com/wiki/index.php/Microsoft_KB_Archive/298593)。 - nwsmith

1

这是使用WHS脚本另一种实现方式:

Set objWMIService = GetObject("Winmgmts:root\cimv2")

For Each objComputer in _
    objWMIService.InstancesOf("Win32_ComputerSystem")

    objComputer.rename "NewComputerName", NULL, NULL 
Next

源代码


0
如果你想在Windows 10 IoT上进行此操作,那么有一个内置的命令可以使用:
setcomputername [newname]
不幸的是,在完整版本的Windows 10中,这个命令是不存在的。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接