Configuring environment variables with PS

• Question

• Hello,

I've written two simple functions to set a new env variable and check if it was correctly set later. But something is wrong and I can't figure out what!
1- the set function isn't adding any new env variables
2- if I add an evn variable manually, the check function says it doesn't exist.

function IVU-Set-EnvVariable {
param(
[Parameter(Mandatory = $true)] [String]$name,
[Parameter(Mandatory=$true)] [String]$path
)
[Environment]::SetEnvironmentVariable("$name", "$path", "Machine")

$checkEntry = [Environment]::GetEnvironmentVariable("$name", "Machine")
if ($checkEntry -eq$path) {
Write-Host "System environment variable $name set." } else { Write-Host "System environment variable$name could not be set." "Error"
}
}

function Check-EnvVariable {
param(
[Parameter(Mandatory = $true)] [String]$name,
[Parameter(Mandatory=$true)] [String]$path
)
$checkEntry = [Environment]::GetEnvironmentVariable("$name", "Machine")
if ($checkEntry -eq$path) {
return $true } else { return$false
}
}

Could someone please take a look and tell me how to fix these bugs? I'm fairly new to PowerShell.

Tuesday, January 7, 2020 3:41 PM

All replies

\_(ツ)_/

Tuesday, January 7, 2020 4:08 PM
• This is how to use the methods and they require elevation to alter the "Machine" variables.

$value = 200$name='testme'
[Environment]::SetEnvironmentVariable($name,$value, 'Machine')
[Environment]::GetEnvironmentVariable($name,'Machine') It is not a good practice to out quotes around variables. There is no need to use a function for this. Failures will cause exceptions and the function will fail. \_(ツ)_/ • Edited by Tuesday, January 7, 2020 4:14 PM Tuesday, January 7, 2020 4:13 PM • I hope it's better now. Sorry for the inconvenience Tuesday, January 7, 2020 11:12 PM • I hope it's better now. Sorry for the inconvenience Thank you. Remember that the forum is so others can also benefit from the threads. Browsers do not display code well and make copying nearly impossible with some browsers. The code box helps that. \_(ツ)_/ Tuesday, January 7, 2020 11:17 PM • This is how to use the methods and they require elevation to alter the "Machine" variables. $value = 200
$name='testme' [Environment]::SetEnvironmentVariable($name, $value, 'Machine') [Environment]::GetEnvironmentVariable($name,'Machine')

It is not a good practice to out quotes around variables.

There is no need to use a function for this.  Failures will cause exceptions and the function will fail.

\_(ツ)_/

Since I'm trying to automate the installation of a program I need this as a function. Not using quotes didn't help... do you have other ideas?
Thursday, January 9, 2020 7:23 AM
• \to automate the installation of a program I need this as a function. Not using quotes didn't help... do you have other ideas?

This is a function.  Why wrap it in another function.  You must run as an administrator and run elevated to create a machine environment variable.  Th

e method will throw an exception if there is an issue.  No exception and it has created the variable.  Creating a variable in the machine environment will not make it visible in the current session.  You must exit and start a new session for it to be visible.

PS C:\scripts> [Environment]::SetEnvironmentVariable('test', '$path', 'Process') PS C:\scripts> dir env:test Name Value ---- ----- test$path

PS C:\scripts>


Your issue is lack of basic technical training in Windows and no training in PowerShell.  Take some time to research what environment variables are and how they work.  There are many articles on the MS site.  If you just want the answer then the answer is that you can't do what you are trying to do the way you are trying to do it.   How you need to do this depends on what its purpose is.  If you are creating an installer then the installer API has methods for creating environment variables and it can easily configure the system however, it still cannot make a new machine variable visible in the current session.

Wrapping a function in a function will not change this behavior.  Perhaps if you can explain why you need this we can help you find a way to accomplish your task.

\_(ツ)_/

Thursday, January 9, 2020 8:58 AM