• Hi there and welcome to PC Help Forum (PCHF), a more effective way to get the Tech Support you need!
    We have Experts in all areas of Tech, including Malware Removal, Crash Fixing and BSOD's , Microsoft Windows, Computer DIY and PC Hardware, Networking, Gaming, Tablets and iPads, General and Specific Software Support and so much more.

    Why not Click Here To Sign Up and start enjoying great FREE Tech Support.

    This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Solved Adding/Removing Environment Variables Through Bat FIle

Status
Not open for further replies.
Hello there. I have added some environment variables to my system by writing this txt file, then executing it as a bat file. Was wondering how I could rewrite the same txt file to remove those environment variables.

Best regards,

Txt file contents:

@echo off
setx UGII_ENV_FILE "C:\Users\Kevin.Saginian\Desktop\Kevin"


setx UGII_SDI_BPS_JOBS_DIR C:\Temp
setx UGII_SDI_OVERRIDE_HOME C:\Temp
setx UGII_SDI_OUTPUT_DIR C:\Temp
 

Attachments

  • Capture.PNG
    Capture.PNG
    23.4 KB · Views: 8
SETX differs from SET in that it is meant to permanently set variables.
like the SET command, SETX var "" will clear it but it remains in the Registry.

you can delete SETX created variables via Control Panel > System > Advanced tab > Environment Variable button.

if you wanted to do that from a .BAT file, you would use - reg delete hkcu\environment /f /v {your variable} if you set it as a User variable.
 
  • Like
Reactions: kmsaginian
Thank you! How about if I set it as a system variable?

Clearing the user variable worked with:
REG delete “HKCU\Environment” /F /V “UGII_ENV_FILE”

But I'm trying to clear the system variable with:
REG delete "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /F /V "UGII_ENV_FILE"
and it is not working, telling me "ERROR: Access is Denied"
 
Are there any syntax differences between trying to execute the REG delete through a batch file vs directly through the command prompt?

Thank you for your continued input.
 
Thank you. One other thing I am encountering is this: When I execute the bat file to add, the command prompt shows up and states the variables have been added. When I execute the bat to remove (as an admin), the command prompt flashes for a second and no changes have been made. Any ideas what's going wrong there?

Again thank you lol I shouldn't have any more questions after this
 
I have this at the top of .BAT files I need to run with admin rights.

@echo off
net session >nul 2>&1
if not %errorlevel%==0 (
echo CreateObject^("Shell.Application"^).ShellExecute "%~d0%~p0%~n0.bat", "", "", "runas", 1 > "%temp%\temp.vbs"
cscript //nologo "%temp%\temp.vbs"
exit /b
)


see if that helps.
 
  • Like
Reactions: kmsaginian
quoted from @kmsaginian
Good afternoon gentlemen.

I just wanted to apologize for my lack of response in the Q/A we had going titled "Adding/Removing Environment Variables Through Bat FIle." I never intended to disrespect your fine help center, or the people that work in it. In fact, I greatly appreciate this service.

The truth is I was still having trouble, but felt I had already taken up so much of your time/resource that I did not want to continue to press the issue. In retrospect, not responding at all seems to cause more problems doesn't it? haha

I hope you two can accept my apology and allow me to continue to ask questions when they arise.

Thank you gentlemen, your help has been much appreciated.
 
Status
Not open for further replies.