标题: [转载代码] [PowerShell每日技巧]添加、删除环境变量(20140207) [打印本页]
作者: DAIC 时间: 2014-2-18 13:26 标题: [PowerShell每日技巧]添加、删除环境变量(20140207)
PowerShell can read environment variables easily. This returns the current windows folder:复制代码
However, if you want to make permanent changes to user or machine environment variables, you need to access .NET functionality. Here is a simple function that makes setting or deleting environment variables a snap:- function Set-EnvironmentVariable
- {
- param
- (
- [Parameter(Mandatory=$true, HelpMessage='Help note')]
- $Name,
-
- [System.EnvironmentVariableTarget]
- $Target,
-
- $Value = $null
-
- )
-
- [System.Environment]::SetEnvironmentVariable($Name, $Value, $Target )
-
- }
复制代码
To create a permanent user environment variable, try this:- Set-EnvironmentVariable -Name TestVar -Value 123 -Target User
复制代码
Note that new user variables are visible only to newly launched applications. Applications that were already running will keep their copied process set unless they explicitly ask for changed variables.
And here is the line that deletes the variable again:- Set-EnvironmentVariable -Name TestVar -Value '' -Target User
复制代码
http://powershell.com/cs/blogs/tips/archive/2014/02/07/setting-and-deleting-environment-variables.aspx
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |