Adding/Removing Environment Variables Through Bat FIle

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kmsaginian
    PCHF Member
    • Jun 2022
    • 6

    #1

    Adding/Removing Environment Variables Through Bat FIle

    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
  • Bruce
    PCHF Member
    • Oct 2017
    • 10697

    #2
    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.

    Comment

    • Bruce
      PCHF Member
      • Oct 2017
      • 10697

      #3
      @kmsaginian - still need help?

      Comment

      • kmsaginian
        PCHF Member
        • Jun 2022
        • 6

        #4
        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”

        Comment

        • kmsaginian
          PCHF Member
          • Jun 2022
          • 6

          #5
          Also tried changing the user from myself to windows at the start of the command prompt by typing:
          cd C:\WINDOWS\system32

          Comment

          • Bruce
            PCHF Member
            • Oct 2017
            • 10697

            #6
            access denied is usually when you haven’t started the .BAT script with admin privileges.

            Comment

            • kmsaginian
              PCHF Member
              • Jun 2022
              • 6

              #7
              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.

              Comment

              • Bruce
                PCHF Member
                • Oct 2017
                • 10697

                #8
                nope, same syntax.
                batch files are just a bunch of commands that have been batched up into one script.

                Comment

                • kmsaginian
                  PCHF Member
                  • Jun 2022
                  • 6

                  #9
                  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

                  Comment

                  • kmsaginian
                    PCHF Member
                    • Jun 2022
                    • 6

                    #10
                    Removes just fine when I enter it into the Administrative Command Prompt, just not when I try to run it through the bat.

                    Comment

                    • Malnutrition
                      PCHF Moderator
                      • Jul 2016
                      • 7041

                      #11
                      PowerRun is a tool to launch any program/script files with the same privileges as the TrustedInstaller /SYSTEM. it is a Portable and free

                      Comment

                      • Bruce
                        PCHF Member
                        • Oct 2017
                        • 10697

                        #12
                        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.

                        Comment

                        • Bruce
                          PCHF Member
                          • Oct 2017
                          • 10697

                          #13
                          @kmsaginian - still need help?

                          Comment

                          • phillpower2
                            PCHF Administrator
                            • Sep 2016
                            • 15205

                            #14
                            Being that the OP has revisited the forum but chose not to reply this thread will be closed.

                            Comment

                            • Bruce
                              PCHF Member
                              • Oct 2017
                              • 10697

                              #15
                              thread re-opened as per OP request.

                              Comment

                              Working...