从命令行更改 Windows 主机名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/54989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:14:52  来源:igfitidea点击:

Change windows hostname from command line

windowscommand-line

提问by dmo

Is it possible to change the hostname in Windows 2003 from the command line with out-of-the-box tools?

是否可以使用开箱即用的工具从命令行更改 Windows 2003 中的主机名?

采纳答案by Steven Murawski

The netdom.exe command line program can be used. This is available from the Windows XP Support Tools or Server 2003 Support Tools (both on the installation CD).

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

Usage guidelines here

使用指南在这里

回答by Gringo Suave

The previously mentioned wmiccommand is the way to go, as it is installed by default in recent versions of Windows.

前面提到的wmic命令是可行的方法,因为它默认安装在最新版本的 Windows 中。

Here is my small improvement to generalize it, by retrieving the current name from the environment:

这是我通过从环境中检索当前名称来概括它的小改进:

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

NOTE: The command must be given in one line, but I've broken it into two to make scrolling unnecessary. As @rbeede mentions you'll have to reboot to complete the update.

注意:该命令必须在一行中给出,但我已将其分成两行以使滚动变得不必要。正如@rbeede 提到的,您必须重新启动才能完成更新。

回答by litao

cmd (command):

cmd(命令):

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

powershell (windows 2008/2012):

PowerShell(Windows 2008/2012):

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

after that, you need to reboot your computer.

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

回答by axk

Here's another way of doing it with a WHS script:

这是使用 WHS 脚本执行此操作的另一种方法:

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

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

    objComputer.rename "NewComputerName", NULL, NULL 
Next

Source

来源

回答by LaurFromRomania

Why be easy when it can be complicated? Why use third-party applications like netdom.exe when correct interogations is the way? Try 2 interogations:

当它可以复杂时,为什么要容易?当正确的询问是正确的方式时,为什么要使用像 netdom.exe 这样的第三方应用程序?尝试 2 个问询:

wmic computersystem where caption='%computername%' get caption, UserName, Domain /format:value

wmic computersystem where caption='%computername%' 获取标题、用户名、域/格式:值

wmic computersystem where "caption like '%%%computername%%%'" get caption, UserName, Domain /format:value

wmic computersystem where "caption like '%%%computername%%%'" get caption, UserName, Domain /format:value

or in a batch file use loop

或在批处理文件中使用循环

for /f "tokens=2 delims==" %%i in ('wmic computersystem where "Caption like '%%%currentname%%%'" get UserName /format:value') do (echo. UserName- %%i)

for /f "tokens=2 delims==" %%i in ('wmic computersystem where "Caption like '%%%currentname%%%'" get UserName /format:value') do (echo.UserName- %%i )

回答by GEOCHET

I don't know of a command to do this, but you could do it in VBScript or something similar. Somthing like:

我不知道有什么命令可以执行此操作,但您可以在 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" 

Original

原来的

回答by Swapnil Gangrade

Use below command to change computer hostname remotely , Require system reboot after change..

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

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

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

回答by codechurn

If you are looking to do this from Windows 10 IoT, then there is a built in command you can use:

如果您希望从 Windows 10 IoT 执行此操作,则可以使用一个内置命令:

setcomputername [newname]

setcomputername [newname]

Unfortunately, this command does not existin the full build of Windows 10.

不幸的是,此命令在 Windows 10 的完整版本中不存在