gpt4 book ai didi

windows - 您如何在 PowerShell 中获取用户环境变量?

转载 作者:可可西里 更新时间:2023-11-01 11:43:50 26 4
gpt4 key购买 nike

我看到 here ,它可以在 Windows PowerShell 中点源脚本。如何取而代之获取用户的环境变量(或者用户变量保存在哪个文件路径中)?

例子.ps1:

[Environment]::SetEnvironmentVariable("PATH", "C:\newapp\bin", "User")
newapp --do-something # fails until PS restart

最佳答案

环境变量存储在注册表中,而不是文件中¹,并且会在 PowerShell 启动时自动加载。没有什么可以点源的。您可以通过列出 env: PSDrive:

的内容来显示环境变量(用户环境与系统环境合并)
PS C:\> Get-ChildItem env:Name                           Value----                           -----ALLUSERSPROFILE                C:\ProgramDataAPPDATA                        C:\Users\jsmith\AppData\RoamingCommonProgramFiles             C:\Program Files\Common Files...

Environment variables differ from regular PowerShell variables in that they must be accessed via the env scope prefix:

PS C:\> $username = 'foo'    # ← PowerShell variablePS C:\> $usernamefooPS C:\> $env:username        # ← environment variablejsmith

You can modify the current environment by making changes to the variables

PS C:\> $env:Path += ';C:\some\folder'    # append C:\some\folder to PATH

or replacing them entirely:

PS C:\> $env:Path = 'C:\some\folder'      # set PATH to C:\some\folder

It's not recommended to manipulate environment variables like $env:USERNAME, though.

Note, however, that while you can modify environment variables or load them by dot-sourcing a file (using the proper prefix in the variable names) these variables are not persisted as they're applied to the process environment only.

To make persistent changes to environment variables you need to edit the values in the registry

Set-ItemProperty -Path 'HKCU:\Environment' -Name 'foo' -Value 'bar' -Type String

或使用.Net API

[Environment]::SetEnvironmentVariable('foo', 'bar', 'User')

请注意,通过 API 更改注册表值有 pitfalls

使用 setx 命令行实用程序也可以,但我不建议这样做,因为语法不像人们希望的那样简单,而且 PowerShell 首先不需要外部程序。

您还可以通过从注册表中重新读取其值来更新在进程启动后更改的环境变量:

$env:foo = [Environment]::GetEnvironmentVariable('foo', 'User')

但是请注意,Windows 将用户和系统环境存储在不同的位置。如果你有一个像 $env:Path 这样的变量,它在用户和系统环境中都定义了,你需要结合这两个值:

$env:Path = [Environment]::GetEnvironmentVariable('Path', 'Machine') + ';' +
[Environment]::GetEnvironmentVariable('Path', 'User')

如果您需要为当前进程更改的环境变量并且持续存在,您需要执行这两个操作:更改$env:VARIABLE并将更改的变量写回注册表。如果您需要多次执行此操作,建议将这两个操作包装在自定义函数中,例如像这样:

function Set-EnvVariable {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
[String]$Name,
[Parameter(Mandatory=$true)]
$Value,
[Parameter(Mandatory=$false)]
[String]$Type = 'String'
)

if (Test-Path -LiteralPath "env:$Name") {
Set-Item "env:$Name" -Value $Value
} else {
New-Item -Path 'env:' -Name $Name -Value $value
}

Set-ItemProperty -Path 'HKCU:\Environment' -Name $Name -Value $Value -Type $Type
}

¹ 那么,从技术上讲,注册表中包含(除其他外)用户环境变量的部分 存储在用户配置文件目录中的文件 ntuser.dat 中.但是,在 PowerShell 中,这不是什么都可以点源的。

关于windows - 您如何在 PowerShell 中获取用户环境变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51246716/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com