Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign empty system variable using setx

Tags:

batch-file

I need a system variable and assign an empty string value to it. I've tried

setx samplepassword ""

But upon echo %samplepassword% , what should be an empty string is instead %samplepassword%. Now I found this in the docs

Setting value of "" (empty quotes) will appear to delete the variable - it's not shown by SET but the variable name will remain in the registry.

http://ss64.com/nt/setx.html

How can I assign an empty system variable using setx?

Update: I have checked the environment variables via the Windows GUI: Control Panel | System | Advanced | Environment Variables and saw that the DB_PASSWORD was indeed initialized with an empty string value. But why does executing echo %samplepassword% give me the above output? I have opened a new instance of cmd to be precise.

like image 746
Eduard Avatar asked Jul 16 '26 08:07

Eduard


2 Answers

Technically you can create an empty environment variable, but not with setx.

Run that as administrator

reg add "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment"  /v TESTENV /d ""

the registry key is added:

enter image description here

it even shows in environment variables system panel

enter image description here

But not in the list when invoking set

So even if it is possible, it is rather useless. However it could fool some programs testing if the env. variable exists by checking if registry key exists. So it could even be counter-productive and source of confusion.

like image 71
Jean-François Fabre Avatar answered Jul 18 '26 04:07

Jean-François Fabre


Worth pointing out that Windows does not support blank/empty values for environment values.

Try and define a new environment variable from Settings > System > About > Advanced System Settings > Environment Variables. If no value is entered (see image below) then the OK button is disabled.

Cannot define environment variable with blank value

Or try and create the environment variable in code (see C# example below) with a blank (or null) value...and the environment variable is deleted!

// If not existing, the environment variable does NOT get defined
// If it exists, the environment variable is deleted
Environment.SetEnvironmentVariable(
    "TestBlank",
    string.Empty, // Or "null"
    EnvironmentVariableTarget.User);

The minimum length of environment variable values is 1. The maximum length is (2^16 - 2) = 32766 (or Int16.MaxValue - 1 in C#). You must do this in code, though, as the Windows GUI limits you to 2047 characters. Why these limits? Not sure, probably historical (aka "lost in the mists of time").

So if you use set or setx with a blank value, you are actually deleting the variable (which is likely not your intention).

like image 30
AlainD Avatar answered Jul 18 '26 04:07

AlainD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!