Skip to content

ENDLOCAL command in Windows

The Windows endlocal command is used to end the localization of environment variables in batch scripts. It is often paired with the setlocal command to limit the scope of changes made to variables. By using endlocal, you can ensure that any modifications to variables made within a certain scope are reverted once that scope is exited. This command is particularly useful when you want to isolate changes to environment variables within a specific section of a script without affecting the rest of the script or the system’s environment.

Terminal window
endlocal
OptionDescription
N/ANo specific options for the endlocal command.
ParameterDescription
N/AThe endlocal command does not take any parameters.
Terminal window
setlocal
set myVar=Hello World
echo %myVar%
endlocal

This example demonstrates how to store a variable, display its value, and then use the ENDLOCAL command to end the local environment.

Terminal window
setlocal
set myVar=123
echo %myVar%
endlocal
set myVar=
echo %myVar%

In this example, a variable is set and then cleared within a batch script using the ENDLOCAL command to manage the scope of the variable.

Delayed Expansion Mode enabled to access a local variable

Section titled “Delayed Expansion Mode enabled to access a local variable”
Terminal window
setlocal enabledelayedexpansion
set myVar=BatchScript
echo !myVar!
endlocal

Here, Delayed Expansion Mode is enabled to access a local variable, and the ENDLOCAL command is used to exit the local scope.

Terminal window
setlocal
set myVar=Example
(
setlocal
set myVar=NewValue
echo Inner: %myVar%
endlocal
)
echo Outer: %myVar%
endlocal

In this example, the ENDLOCAL command is used in a nested context to control the scope of variables and display the values within and outside the inner scope.

Terminal window
set myVar=Global
setlocal
set myVar=Local
echo %myVar%
endlocal
echo %myVar%

This example showcases the use of the ENDLOCAL command to manage the scope of environment variables within a batch script.

Terminal window
setlocal
set myVar=Test
if not defined myVar (
echo Variable not defined
)
endlocal

Here, a conditional check is made for the existence of a variable before the ENDLOCAL command is used to manage the local scope and display a message based on the condition.

Terminal window
setlocal
set myVar=Initial
for %%a in (1,2,3) do (
set myVar=LoopValue%%a
)
echo %myVar%
endlocal

This example illustrates the use of ENDLOCAL within a loop to control the scope of a variable and display its final value outside the loop.

Terminal window
setlocal
for /f "tokens=*" %%a in (sample.txt) do (
set fileContent=%%a
)
echo %fileContent%
endlocal

Here, the ENDLOCAL command is used after reading the content of a file within a local scope to manage the environment and display the file content afterwards.

To use the endlocal command in Windows, execute the following command:

Terminal window
endlocal

The endlocal command is used to end the localization of environment changes in the current batch script.

How can I use endlocal to reset environment changes in a batch script?

Section titled “How can I use endlocal to reset environment changes in a batch script?”

To reset environment changes and revert to the previous state, you can use endlocal without any arguments like this:

Terminal window
endlocal

How to use endlocal in combination with setlocal to limit environment changes?

Section titled “How to use endlocal in combination with setlocal to limit environment changes?”

To limit the scope of environment changes in a batch script, use setlocal at the beginning and endlocal at the end like this:

Terminal window
setlocal
REM Other commands here
endlocal

Can endlocal be used to undo global system environment changes?

Section titled “Can endlocal be used to undo global system environment changes?”

No, endlocal only affects the local environment changes within the current script or block of code. It does not undo global system environment changes.

Is it necessary to always use endlocal after setlocal in a batch script?

Section titled “Is it necessary to always use endlocal after setlocal in a batch script?”

It is good practice to use endlocal after setlocal in a batch script to ensure proper scoping of environment changes.

How does endlocal differ from setlocal in batch scripting?

Section titled “How does endlocal differ from setlocal in batch scripting?”

While setlocal is used to localize environment changes, limiting their scope, endlocal is used to end this localization and revert to the previous state.

When should I use endlocal in a Windows batch script?

Section titled “When should I use endlocal in a Windows batch script?”

Use endlocal in a batch script when you want to end the localization of environment changes made within a specific script or block of code.

Are there any advanced options or flags for the endlocal command?

Section titled “Are there any advanced options or flags for the endlocal command?”

No, the endlocal command does not have any additional options or flags. It is used simply to end the localization of environment changes in a batch script.

  1. Restores the environment variables scope after using SETLOCAL command.
  2. Ends the local environment changes made within a batch script.