可以通过为变量的值指定空字符串,使用SetEnvironmentVariable()方法删除环境变量。 例如,若要删除环境变量,请执行以下操作Foo: PowerShell复制 [Environment]::SetEnvironmentVariable('Foo','') [Environment]::GetEnvironmentVariable('Foo') Output复制 有关System.Environment类的方法的详细信息,请参阅环境方法。 ...
[Environment]::GetEnvironmentVariable('Foo') new [Environment]::SetEnvironmentVariable('Foo','Bar') update [Environment]::SetEnvironmentVariable('Foo','Tar') delete [Environment]::SetEnvironmentVariable('Foo','') TIPS 添加内容:$Env:Path += ';C:\Tools',在 Windows 中使用 ; 而不是 :。 获...
如果环境变量已经存在,使用SetEnvironmentVariable就是修改它的值。 用法与新建环境变量完全相同。 例如,修改系统环境变量 Path,将上一个例子中JAVA_HOME路径下的bin目录添加到 Path: $path=[Environment]::GetEnvironmentVariable('Path','Machine')$newpath='%JAVA_HOME%'+'\bin;'+$path# 或者:$newpath = $E...
在原有的环境变量基础上新增,并且指定作用域(用户 or 系统 or 会话),一条命令搞定: $addPath=‘c:\add\you\path\here’; $target=‘User’ ; $path = [Environment]::GetEnvironmentVariable(‘Path’, $target); $newPath = $path + ‘;’ + $addPath; [Environment]::SetEnvironmentVariable(“Path”...
$addPath='c:\add\you\path\here';$target='User';$path= [Environment]::GetEnvironmentVariable('Path',$target);if($path-match";$"){$newPath=$path+$addPath; }else{$newPath=$path+';'+$addPath; } [Environment]::SetEnvironmentVariable('Path',$newPath,$target) ...
若要将模块路径添加到PSModulePath环境变量值的模块路径,请使用以下命令格式。 此格式使用System.Environment类的SetEnvironmentVariable方法对PSModulePath环境变量进行与会话无关的更改。 PowerShell #Save the current value in the $p variable.$p= [Environment]::GetEnvironmentVariable("PSModulePath")#Add the ne...
$env:TestVar1="This is my environment variable" 删除和更新环境变量: del env:windir 更新环境变量就直接重新赋值就好了,其实,$env:中的环境变量只是机器环境变量的一个副本,即使你更改了它,下一次重新打开时,又会恢复如初。(.NET方法更新环境变量除外) ...
PS C:\Users\cxxu\Desktop> [Environment]::GetEnvironmentVariable($PA, "Machine") -split ";" D:\repos\ThinkDSP\code D:\repos\CCSER\cxxu_serlib C:\new_path_demo D:\repos\PythonLearn\cxxu_pylib D:\repos\CCSER\SER PS C:\Users\cxxu\Desktop> [Environment]::GetEnvironmentVariable($PA,...
Display the value of the COMPUTERNAME environment variable: Get-Childitem env:computername This can be made easier if you first Set-Location (cd) to the Env: Drive cd env: Then to display a variable: Get-ChildItem computername Returning a string value with $env: PowerShell can also address...
Get-ChildItem Env: Or at least it didn’t show up until we restarted PowerShell. (Or started up a new instance of PowerShell.) However, wecouldretrieve the value of the new variable at any time by using this command: [Environment]::GetEnvironmentVariable("TestVariable","User") ...