简体   繁体   English

使用批处理文件根据配置文件名称删除用户配置文件注册表项

[英]Deleting user profile registry key based on profile name with Batch file

I am attempting to write a batch script to delete a registry key for a user profile.我正在尝试编写一个批处理脚本来删除用户配置文件的注册表项。 The user profile will always have the same name, but the key is different for every computer and increments each time the username is created, even though the previous one was deleted.用户配置文件将始终具有相同的名称,但每台计算机的密钥都不同,并且每次创建用户名时都会增加,即使前一个已被删除。

I'm guessing that it would require some type of for loop to identify the key value for the ProfileImagePath as C:\Users\Username.我猜它需要某种类型的 for 循环来将 ProfileImagePath 的键值标识为 C:\Users\Username。

I know wildcards don't work so reg delete 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\S-1-5-21-*' /f won't work.我知道通配符不起作用,所以 reg delete 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\S-1-5-21-*' /f 不起作用。

I'm aware of some PS scripts that can do this, but I would like to keep this in a batch file.我知道一些 PS 脚本可以做到这一点,但我想把它保存在一个批处理文件中。

Here's a single line example of the methodology I'd suggest, (which will not become obsolete when WMIC.exe is removed from Windows 11).这是我建议的方法的单行示例(从 Windows 11 中删除WMIC.exe时不会过时)。

@For /F "EOL= Delims=" %%G In ('%SystemRoot%\System32\reg.exe Query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" /S /F "C:\Users\Username" /D /E 2^>NUL ^| %SystemRoot%\System32\find.exe "S-1-5-21-"') Do @%SystemRoot%\System32\reg.exe Delete "%%G" /V "ProfileImagePath" /F 1>NUL 2>&1

Please take account of my commented advice too.请也考虑我的评论建议

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM