首页 > 解决方案 > 如何运行 PowerShell 命令以删除批处理脚本中持久存储的用户环境变量?

问题描述

如何在批处理文件中运行以下 PowerShell 命令以删除持久存储的用户环境变量PCM_STYLE

[Environment]::SetEnvironmentVariable("PCM_STYLE",$null,"User")

标签: batch-file

解决方案


如果您确实需要为此使用 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 )

推荐阅读