windows - 通过批处理 cmd 重命名/删除注册表中的键

标签 windows batch-file cmd registry wmic

当 Windows 7 中的用户登录并且配置文件已损坏时,他们将获得空白配置文件。我可以通过转到注册表并删除临时配置文件、设置引用计数和状态值来轻松修复。我被困在我的批处理中,因为一旦我在批处理中调用它,我从注册表中提取的内容就会在注册表项名称中添加 2 个空格。如果我回显 %SID%,它会返回没有可见空间的正确值。注册表确实有 %SID% 和 %SID%.bak

@echo off
setlocal ENABLEDELAYEDEXPANSION
for /F "skip=1 delims=" %%j in ('wmic useraccount where name^="%username%" get SID') do (
  set SID=%%j
  goto :DONE
)
:DONE
echo  SID=%SID%

reg delete "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\%SID%.bak"
pause

批处理结果:

 SID=S-1-5-21-1559011707-81249799-2450556423-1000
Permanently delete the registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Window
s NT\CurrentVersion\ProfileList\S-1-5-21-1559011707-81249799-2450556423-1000  .b
ak (Yes/No)? y
ERROR: The system was unable to find the specified registry key or value.
Press any key to continue . . .

如您所见,输出在 SID 和文件扩展名之后放置了 2 个空格

有什么想法吗?

最佳答案

WMI 查询结果以 UCS-2 LE 而非 ANSI 编码。在捕获 wmic 的输出时,我发现有时向查询添加一次性列并使用 /format:csv 保留格式会很有帮助。

@echo off
setlocal

for /f "tokens=2 delims=," %%I in (
    'wmic useraccount where "name='%username%'" get SID^,Status /format:csv'
) do set "SID=%%I"

rem // echo result
set SID

关于windows - 通过批处理 cmd 重命名/删除注册表中的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32615301/

相关文章:

python - Pip - 启动器中的 fatal error : Unable to create process using '"'

windows - GVIM - 保存窗口配置

java - 如何构建一个位于当前文件夹下一个目录的文件夹

windows - 同一端口上的两个监听器?

c++ - 如何在 C++ 中将 double 转换为 REFERENCE_TIME?

batch-file - 如果时间在指定时间段内,则设置批处理文件变量

Android 调试桥 adb shell

batch-file - 如何使用 .bat 文件运行传递 docker 终端命令

java - Windows cmd 中的 JVM http 代理

lucene - 如何在cmd中使用Lucene