Windows 批处理自动根据当前日期时间修改计算机名

0 Comment

如题,Windows 批处理自动根据当前日期时间修改计算机名。

代码如下:

@echo off  
echo 修改计算机器名...
set pcname=Oo-%date:~2,2%%date:~5,2%%date:~8,2%%time:~0,2%%time:~3,2%%time:~6,2%
echo Windows Registry Editor Version 5.00>ComputerName.reg
echo [HKEY_CURRENT_USER\Software\Microsoft\Windows\ShellNoRoam]>>ComputerName.reg
echo @="%pcname%">>ComputerName.reg
echo [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName]>>ComputerName.reg
echo [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName]>>ComputerName.reg
echo "ComputerName"="%pcname%">>ComputerName.reg
echo [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName]>>ComputerName.reg
echo "ComputerName"="%pcname%">>c:\TempInfo.reg>>ComputerName.reg
echo [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog]>>ComputerName.reg
echo "ComputerName"="%pcname%">>ComputerName.reg
echo [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName]>>ComputerName.reg
echo "ComputerName"="%pcname%">>ComputerName.reg
echo [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters]>>ComputerName.reg
echo "NV Hostname"="%pcname%">>ComputerName.reg
echo "Hostname"="%pcname%">>ComputerName.reg
echo [HKEY_USERS\.DEFAULT\Software\Microsoft\Windows\ShellNoRoam]>>ComputerName.reg
echo @="%pcname%">>ComputerName.reg
regedit /s ComputerName.reg
del /q ComputerName.reg
echo 修改计算机器名称完成...

亲测 Win10、Win7 可用,上面代码修改计算机名如:Oo-181011215503,无需重启即可修改计算机名。

注意:计算机名不能超 15 字符,注册表强行修改并重启电脑会导致系统崩溃无法启动(Win10 亲测)。

或者随机生成 15 位字母数字组合作为计算机名:

随机数生成代码来自:此处

@echo off & setlocal enabledelayedexpansion 
echo 修改计算机器名...

set str=ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890

for /l %%a in (1 1 15) do (
set /a n=!random!%%36
call set a=%%str:~!n!,1%%!a!
)
set pcname=%a%
echo Windows Registry Editor Version 5.00>ComputerName.reg
echo [HKEY_CURRENT_USER\Software\Microsoft\Windows\ShellNoRoam]>>ComputerName.reg
echo @="%pcname%">>ComputerName.reg
echo [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName]>>ComputerName.reg
echo [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName]>>ComputerName.reg
echo "ComputerName"="%pcname%">>ComputerName.reg
echo [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName]>>ComputerName.reg
echo "ComputerName"="%pcname%">>c:\TempInfo.reg>>ComputerName.reg
echo [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog]>>ComputerName.reg
echo "ComputerName"="%pcname%">>ComputerName.reg
echo [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName]>>ComputerName.reg
echo "ComputerName"="%pcname%">>ComputerName.reg
echo [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters]>>ComputerName.reg
echo "NV Hostname"="%pcname%">>ComputerName.reg
echo "Hostname"="%pcname%">>ComputerName.reg
echo [HKEY_USERS\.DEFAULT\Software\Microsoft\Windows\ShellNoRoam]>>ComputerName.reg
echo @="%pcname%">>ComputerName.reg
regedit /s ComputerName.reg
del /q ComputerName.reg
echo 修改计算机器名称完成...

代码修改自:http://www.5x54.com/article/html/200709/27114128.html

声明:除非注明,常阳时光文章均为原创
本文地址:https://cyhour.com/897/
转载时必须以链接形式注明原始出处。
Views: 276 Tags:  ,  , 

Comments:0

发表留言

炒蛋:Vultr 送$25 | 搬瓦工年付$19补货,优惠码 BWH26FXH3HIQ | 推荐VPS信息