Andr谩s T贸th‘s professional blog
banditoth.net

Hey there 馃憢, I’m banditoth a .NET MAUI developer from Hungary.
I write about software development with .NET technologies.

You can find me on:
LinkedIn | Github | StackOverflow | X / Twitter | Threads

Windows: Set Environment variables for users

This content has 3 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.

For some cases you might need to set environment variables for your .NET application.
This is true to .NET core too, when you are storing your application settings in these variables.

How to set up a script for the job

It’s handy to create a windows command line script for creating these variables, because when you are on a new work environment, than you don’t need to look up the keys and values.

So bring up your most-loved text editor, make your environment_variable_creator.bat, and add the following code:

@echo on
SETX VARIABLE1 VALUE
SETX PREFIX_VARIABLE2 true
pause

If you are using .NET core’s configuration builder with a prefix like this:

				ConfigurationBuilder cfgBuilder = new ConfigurationBuilder();
				cfgBuilder.AddEnvironmentVariables("PREFIX_");
				IConfigurationRoot configuration = cfgBuilder.Build();
				var value = configuration["VARIABLE2"];

Then you need to set your variables with prefix “PREFIX_” in the bat file. In this scenario, VARIABLE1 can not be read, because it does not contain the prefix.

The code sets the variables only for your user account, not for others. You can parameterize the SETX command to set the settings for a different user, but i recommend, to run the script with a command line for the targeted user, because its much easier.

To run your script under other users name, then open your command prompt with ‘Run as… different user’

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.