我发现设置PATH环境变量只影响旧的命令提示符。PowerShell似乎有不同的环境设置。如何更改PowerShell (v1)的环境变量?
注意:
我希望我的更改是永久性的,这样我就不必每次运行PowerShell时都设置它。PowerShell有配置文件吗?比如Unix上的Bash配置文件?
我发现设置PATH环境变量只影响旧的命令提示符。PowerShell似乎有不同的环境设置。如何更改PowerShell (v1)的环境变量?
注意:
我希望我的更改是永久性的,这样我就不必每次运行PowerShell时都设置它。PowerShell有配置文件吗?比如Unix上的Bash配置文件?
当前回答
在PowerShell中,可以通过输入以下命令导航到环境变量目录:
Set-Location Env:
这将把您带到Env:>目录。在这个目录中:
要查看所有环境变量,输入:
Env:\> Get-ChildItem
要查看特定的环境变量,输入:
Env:\> $Env:<variable name>, e.g. $Env:Path
要设置一个环境变量,输入:
Env:\> $Env:<variable name> = "<new-value>", e.g. $Env:Path="C:\Users\"
要删除一个环境变量,输入:
Env:\> remove-item Env:<variable name>, e.g. remove-item Env:SECRET_KEY
更多信息请参见关于环境变量。
其他回答
正如Jonathan Leaders在这里提到的,运行提升的命令/脚本来改变“machine”的环境变量是很重要的,但是运行一些提升的命令并不一定要用社区扩展来完成,所以我想在某种程度上修改和扩展JeanT的回答,即使脚本本身没有运行提升,也可以改变机器变量:
function Set-Path ([string]$newPath, [bool]$permanent=$false, [bool]$forMachine=$false )
{
$Env:Path += ";$newPath"
$scope = if ($forMachine) { 'Machine' } else { 'User' }
if ($permanent)
{
$command = "[Environment]::SetEnvironmentVariable('PATH', $env:Path, $scope)"
Start-Process -FilePath powershell.exe -ArgumentList "-noprofile -command $Command" -Verb runas
}
}
如果在PowerShell会话期间,您需要查看或临时修改PATH环境变量,您可以键入以下命令之一:
$env:Path # shows the actual content
$env:Path = 'C:\foo;' + $env:Path # attach to the beginning
$env:Path += ';C:\foo' # attach to the end
只有将值推入注册表的答案才会影响永久更改(因此这个线程上的大多数答案,包括已接受的答案,不会永久影响Path)。
下面的函数适用于Path / PSModulePath和User / System类型。默认情况下,它还会将新路径添加到当前会话。
function AddTo-Path {
param (
[string]$PathToAdd,
[Parameter(Mandatory=$true)][ValidateSet('System','User')][string]$UserType,
[Parameter(Mandatory=$true)][ValidateSet('Path','PSModulePath')][string]$PathType
)
# AddTo-Path "C:\XXX" "PSModulePath" 'System'
if ($UserType -eq "System" ) { $RegPropertyLocation = 'HKLM:\System\CurrentControlSet\Control\Session Manager\Environment' }
if ($UserType -eq "User" ) { $RegPropertyLocation = 'HKCU:\Environment' } # also note: Registry::HKEY_LOCAL_MACHINE\ format
$PathOld = (Get-ItemProperty -Path $RegPropertyLocation -Name $PathType).$PathType
"`n$UserType $PathType Before:`n$PathOld`n"
$PathArray = $PathOld -Split ";" -replace "\\+$", ""
if ($PathArray -notcontains $PathToAdd) {
"$UserType $PathType Now:" # ; sleep -Milliseconds 100 # Might need pause to prevent text being after Path output(!)
$PathNew = "$PathOld;$PathToAdd"
Set-ItemProperty -Path $RegPropertyLocation -Name $PathType -Value $PathNew
Get-ItemProperty -Path $RegPropertyLocation -Name $PathType | select -ExpandProperty $PathType
if ($PathType -eq "Path") { $env:Path += ";$PathToAdd" } # Add to Path also for this current session
if ($PathType -eq "PSModulePath") { $env:PSModulePath += ";$PathToAdd" } # Add to PSModulePath also for this current session
"`n$PathToAdd has been added to the $UserType $PathType"
}
else {
"'$PathToAdd' is already in the $UserType $PathType. Nothing to do."
}
}
# Add "C:\XXX" to User Path (but only if not already present)
AddTo-Path "C:\XXX" "User" "Path"
# Just show the current status by putting an empty path
AddTo-Path "" "User" "Path"
在@ali Darabi的回答中编辑注册表键对我来说是最好的,但是 当我没有Powershell的权限时。所以我直接在regedit里编辑。
我想在这个回答中进一步展开这个主题。
重新启动Powershell也不足以传播更改。我必须打开任务管理器并重新启动explorer.exe以触发注册表的重新加载。
导航注册表可能相当乏味,所以为了保持用户友好的体验,你可以从Powershell执行这个:
REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit" /v "LastKey" /d "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment" /f; regedit
它将最后打开的窗口设置为某个注册表路径,这样当您下次打开regedit时,它将以适当的键打开。
无痛苦的、一行的示例解决方案
尝试这三个命令来练习在PowerShell中设置和删除环境变量。
使用注意事项:
在提升的PowerShell上运行这些命令(例如,具有管理员权限)。 在每一步之后,为了使您的命令工作,关闭会话并再次打开它。
添加/创建永久环境变量:
[Environment]::SetEnvironmentVariable("MyEnvVar", "NewEnvValue", "Machine")
Machine是一个环境变量目标,将应用于当前和未来的用户,而不是User目标。
修改/改变环境变量:
[Environment]::SetEnvironmentVariable("MyEnvVar", "NewerEnvValue", "Machine")
删除/删除变量:
[Environment]::SetEnvironmentVariable("MyEnvVar", "", "Machine")