PoshCode Archive  Artifact Content

Artifact fa5e2fe362d55c54470e2bd90ca0091694cf222ca8ed659b65ce348731794759:

  • File Set-EnvironmentVariable.ps1 — part of check-in [0ef0c12c6c] at 2018-06-10 12:56:43 on branch trunk — Allows you to easily create machine-level environment variables that will persist after reboots. Note that the variables created this way may not be visible in the variable: or even env: providers until you restart your Powershell session. Allowable values to Target param are “process”, “user”, and “machine”. (user: halr9000 size: 1292)

# encoding: ascii
# api: powershell
# title: Set-EnvironmentVariable
# description: Allows you to easily create machine-level environment variables that will persist after reboots.  Note that the variables created this way may not be visible in the variable: or even env: providers until you restart your Powershell session.  Allowable values to Target param are “process”, “user”, and “machine”.
# version: 0.1
# type: function
# author: halr9000
# license: CC0
# function: Set-EnvironmentVariable
# x-poshcode-id: 1243
# x-archived: 2012-07-30T02:42:56
# x-published: 2009-07-29T09:15:00
#
# Usage: 
# set-environmentvariable -name Test -value “test value” -target machine -passthru
# TODO: Needs v2 autohelp
#
#requires -version 2

function Set-EnvironmentVariable {
	param (
		[String] [Parameter( Position = 0, Mandatory = $true )] $Name,
		[String] [Parameter( Position = 1, Mandatory = $true )] $Value,
		[EnvironmentVariableTarget] 
			[Parameter( Position = 2 )]
			$Target = [EnvironmentVariableTarget]::Process, 
		[switch] $Passthru
	)
	[environment]::SetEnvironmentVariable( $Name, $Value, $Target )
	if ( $Passthru ) {
		$result = [environment]::GetEnvironmentVariable( $Name, $Target )
		Write-Output @{ $Name = $Result }
	}
}