Avatar of RIAS
RIAS
Flag for United Kingdom of Great Britain and Northern Ireland asked on

Script to set env variables

How to write a script for env variables for system
PowershellScripting Languages

Avatar of undefined
Last Comment
RIAS

8/22/2022 - Mon
oBdA

You don't need a script.
PS C:\> [Environment]::SetEnvironmentVariable

OverloadDefinitions
-------------------
static void SetEnvironmentVariable(string variable, string value)
static void SetEnvironmentVariable(string variable, string value, System.EnvironmentVariableTarget target)

Open in new window

Example:
[Environment]::SetEnvironmentVariable('SomeVariableName', 'SomeValue', 'Machine')

Open in new window

This will set a persistent variable, but only for new processes.
If you want it in the current session as well, run the same command again, but with "Process" as target instead of System.
[Environment]::SetEnvironmentVariable('SomeVariableName', 'SomeValue', 'Process')

Open in new window

To remove the variable, use $null as value:
[Environment]::SetEnvironmentVariable('SomeVariableName', $null, 'Machine')

Open in new window

RIAS

ASKER
Spot on will try and brb
RIAS

ASKER
Worked  perfectly! Thanks
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
RIAS

ASKER
How to do this in bulk. Example: I have 10 env variables to set.
Thanks
oBdA

Depends - if you have 10 different variables to set, they'll probably have 10 different values as well, so how/where is the information coming from?
RIAS

ASKER
so what I did is that just repeated the lines one after the other. Example
[Environment]::SetEnvironmentVariable …
[Environment]::SetEnvironmentVariable…


And it worked.
Quick question, how can I change the variablename from uppercase to lowercase.
Example: Need to change ‘Path’ to ‘path’

Thanks
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
RIAS

ASKER
sorry edited the above comment as there was typo
oBdA

The case of an environment variable name doesn't matter for Windows, so it will just change the value.
If you need to change the case of an existing variable, you need to delete the existing one first (by using $null as value).
RIAS

ASKER
sure, but, how can I can change the variablename from Path to path. It is a required step. Hope there is a way to do so in powershell?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER CERTIFIED SOLUTION
oBdA

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
RIAS

ASKER
Perfect and amazing as always!!!
RIAS

ASKER
Excellent solution provided!!!
RIAS

ASKER
Thanks a ton
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
RIAS

ASKER
Is ‘machine’ equivalent to ‘system’ Just confirming. Also how do I save this as a script?
RIAS

ASKER
cannot see the file option when I open powershell as admin?
oBdA

Yes, "Machine" in this context means "System" as used in the GUI.
You save it as Whatever.ps1 in the text editor of your choice.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
RIAS

ASKER
Thanks again. But notepad doesn’t save it as powershell script.
RIAS

ASKER
it still opens in notepad and not in powershell admin prompt mode
oBdA

Configure Explorer to show file extensions and save again, or enclose the name (including the .ps1 extension) into double quotes when saving in Notepad.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
RIAS

ASKER
Thanks again.
RIAS

ASKER
The case change of Environmental name did not work.

I have an env name ‘Path’ with a variable value : C:/
need to change it to
env name ‘path’ with a variable value ‘C:/‘
RIAS

ASKER
your earlier suggestion just changes the variable value and not the variable name
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
oBdA

It might not work with Path that way, since this is a special environment variable; it can be defined both in User and Machine context and their values will be joined if it's found in both.
You can try to reset the the variable for both "Machine" and "User" (which will then obviously only work for the account you're running this under), but that might not work, or only until a reboot.
But then again: which kind of software insists, on an OS where environment variable names are case insensitive by default, on a lower case variable that is part of the OS and has been named "Path" with an uppercase P basically for forever and a day?
Not to mention that setting Path to C:\ will cause all kinds of problems, what with the Windows and System32 directories missing ...
RIAS

ASKER
Definitely I agree. But, looks like it a part of the process to change Path  to path. But can be achieved manually.

Thanks