batch-file - 如何运行 PowerShell 命令以删除批处理脚本中持久存储的用户环境变量?
问题描述
如何在批处理文件中运行以下 PowerShell 命令以删除持久存储的用户环境变量PCM_STYLE
?
[Environment]::SetEnvironmentVariable("PCM_STYLE",$null,"User")
解决方案
如果您确实需要为此使用 PowerShell,则可以执行您在问题中显示的命令,如下所示:
@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -Command "[Environment]::SetEnvironmentVariable(\"PCM_STYLE\", $null, \"User\")"
但是,最好先使用If
条件检查它是否存在:
@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -Command "If ([Environment]::GetEnvironmentVariable(\"PCM_STYLE\", \"User\")) { [Environment]::SetEnvironmentVariable(\"PCM_STYLE\", $null, \"User\") }
但是,您可以在实用程序的帮助下删除注册表中的环境变量,而无需使用 PowerShell reg.exe
:
@"%SystemRoot%\System32\reg.exe" Delete "HKCU\Environment" /V "PCM_STYLE" /F
而且,和以前一样,最好先确定它是否存在(也使用 reg.exe):
@"%SystemRoot%\System32\reg.exe" Query "HKCU\Environment" /V "PCM_STYLE" 1>NUL 2>&1 && ( "%SystemRoot%\System32\reg.exe" Delete "HKCU\Environment" /V "PCM_STYLE" /F )
推荐阅读
- validation - Grails 4 hasone 没有正确验证
- unity3d - 保存技能树
- linux - 如何从 Bash 中的字符串计算上个月和上年
- c# - UWP WebView - OnScriptNotify 失败的侦听器事件
- python - Pandas - make new column with mean values of the part of another column
- java - How to get rid of white spaces in the list of an array?
- ruby-on-rails - FactoryBot on model with unique validation ... can't rerun tests
- javascript - 使用 React 钩子保持按钮激活/停用状态
- matlab - Adjust regionprops orientation to 0-360 degrees (Matlab)
- r - 如何在使用 rvest::html_table() 解析表格时根据单元格属性操作单个单元格值