[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[转载代码] [PowerShell每日技巧]添加、删除环境变量(20140207)

PowerShell can read environment variables easily. This returns the current windows folder:
  1. $env:windir
复制代码
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:
  1. function Set-EnvironmentVariable
  2. {
  3.     param
  4.     (
  5.         [Parameter(Mandatory=$true, HelpMessage='Help note')]
  6.         $Name,
  7.         [System.EnvironmentVariableTarget]
  8.         $Target,
  9.         $Value = $null
  10.     )
  11.     [System.Environment]::SetEnvironmentVariable($Name, $Value, $Target )
  12. }
复制代码
To create a permanent user environment variable, try this:
  1. 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:
  1. Set-EnvironmentVariable -Name TestVar -Value '' -Target User
复制代码
http://powershell.com/cs/blogs/tips/archive/2014/02/07/setting-and-deleting-environment-variables.aspx

返回列表