SET: Displays, sets, or removes Windows environment variables.


 


... The examples for the command "SET"
... "SET" Excerpt from Microsoft Windows Help
... Important information, tips for the "SET" command

The command: "SET" is on Windows 12, 11, 10, .. , MS Server 2025, 2022, 2019, .. available

The examples for the command "SET"

The `SET` command in the Windows Command Prompt is used to create, view or change environment variables. Here are some examples of using the `SET` command: Example 1: Create a new environment variable:

SET NEW_VARIABLE=Content

Description: This command creates a new environment variable with name `NEW_VARIABLE` and value `content`. Example 2: Display an existing environment variable:

SET EXISTING_VARIABLE

Description: This command displays the value of the environment variable `EXISTING_VARIABLE`. Example 3: Show all environment variables:

SET

Description: Entering `SET` without a specific variable will display all environment variables and their values. Example 4: Delete variable:

SET NEW_VARIABLE=

Description: Here the value of the environment variable `NEUE_VARIABLE` is deleted. Example 5: Using a variable in a batch file:

ECHO %NEW_VARIABLE%

Description: This command prints the value of the environment variable `NEW_VARIABLE`. In batch files, the `%Variable%` syntax is used to access the value of an environment variable. Example 6: Save user input to a variable:

SET /P USER INPUT=Enter something:

Description: This command prompts the user to enter something and stores the input in the `USER INPUT` environment variable. There are many advanced options and features of the `SET` command, including modifications to strings, use of environment variables in batch scripts, and more. Entering `SET /?` in the command prompt will give you a list of available options and more detailed information.

"SET" Excerpt from Microsoft Windows Help

Microsoft Windows [Version 10.0.19045.3693]
(c) Copyright 1985-2023 Microsoft Corp.

C:\\WINDOWS>

Displays, sets, or removes cmd.exe environment variables.

SET [variable=[string]]

  variable  Specifies the environment-variable name.
  string    Specifies a series of characters to assign to the variable.

Type SET without parameters to display the current environment variables.

If Command Extensions are enabled SET changes as follows:

SET command invoked with just a variable name, no equal sign or value
will display the value of all variables whose prefix matches the name
given to the SET command.  For example:

    SET P

would display all variables that begin with the letter 'P'

SET command will set the ERRORLEVEL to 1 if the variable name is not
found in the current environment.

SET command will not allow an equal sign to be part of the name of
a variable.

Two new switches have been added to the SET command:

    SET /A expression
    SET /P variable=[promptString]

The /A switch specifies that the string to the right of the equal sign
is a numerical expression that is evaluated.  The expression evaluator
is pretty simple and supports the following operations, in decreasing
order of precedence:

    ()                  - grouping
    ! ~ -               - unary operators
    * / %               - arithmetic operators
    + -                 - arithmetic operators
    << >>               - logical shift
    &                   - bitwise and
    ^                   - bitwise exclusive or
    |                   - bitwise or
    = *= /= %= += -=    - assignment
      &= ^= |= <<= >>=
    ,                   - expression separator

If you use any of the logical or modulus operators, you will need to
enclose the expression string in quotes.  Any non-numeric strings in the
expression are treated as environment variable names whose values are
converted to numbers before using them.  If an environment variable name
is specified but is not defined in the current environment, then a value
of zero is used.  This allows you to do arithmetic with environment
variable values without having to type all those % signs to get their
values.  If SET /A is executed from the command line outside of a
command script, then it displays the final value of the expression.  The
assignment operator requires an environment variable name to the left of
the assignment operator.  Numeric values are decimal numbers, unless
prefixed by 0x for hexadecimal numbers, and 0 for octal numbers.
So 0x12 is the same as 18 is the same as 022. Please note that the octal
notation can be confusing: 08 and 09 are not valid numbers because 8 and
9 are not valid octal digits.

The /P switch allows you to set the value of a variable to a line of input
entered by the user.  Displays the specified promptString before reading
the line of input.  The promptString can be empty.

Environment variable substitution has been enhanced as follows:

    %PATH:str1=str2%

would expand the PATH environment variable, substituting each occurrence
of "str1" in the expanded result with "str2".  "str2" can be the empty
string to effectively delete all occurrences of "str1" from the expanded
output.  "str1" can begin with an asterisk, in which case it will match
everything from the beginning of the expanded output to the first
occurrence of the remaining portion of str1.

May also specify substrings for an expansion.

    %PATH:~10,5%

would expand the PATH environment variable, and then use only the 5
characters that begin at the 11th (offset 10) character of the expanded
result.  If the length is not specified, then it defaults to the
remainder of the variable value.  If either number (offset or length) is
negative, then the number used is the length of the environment variable
value added to the offset or length specified.

    %PATH:~-10%

would extract the last 10 characters of the PATH variable.

    %PATH:~0,-2%

would extract all but the last 2 characters of the PATH variable.

Finally, support for delayed environment variable expansion has been
added.  This support is always disabled by default, but may be
enabled/disabled via the /V command line switch to CMD.EXE.  See CMD /?

Delayed environment variable expansion is useful for getting around
the limitations of the current expansion which happens when a line
of text is read, not when it is executed.  The following example
demonstrates the problem with immediate variable expansion:

    set VAR=before
    if "%VAR%" == "before" (
        set VAR=after
        if "%VAR%" == "after" @echo If you see this, it worked
    )

would never display the message, since the %VAR% in BOTH IF statements
is substituted when the first IF statement is read, since it logically
includes the body of the IF, which is a compound statement.  So the
IF inside the compound statement is really comparing "before" with
"after" which will never be equal.  Similarly, the following example
will not work as expected:

    set LIST=
    for %i in (*) do set LIST=%LIST% %i
    echo %LIST%

in that it will NOT build up a list of files in the current directory,
but instead will just set the LIST variable to the last file found.
Again, this is because the %LIST% is expanded just once when the
FOR statement is read, and at that time the LIST variable is empty.
So the actual FOR loop we are executing is:

    for %i in (*) do set LIST= %i

which just keeps setting LIST to the last file found.

Delayed environment variable expansion allows you to use a different
character (the exclamation mark) to expand environment variables at
execution time.  If delayed variable expansion is enabled, the above
examples could be written as follows to work as intended:

    set VAR=before
    if "%VAR%" == "before" (
        set VAR=after
        if "!VAR!" == "after" @echo If you see this, it worked
    )

    set LIST=
    for %i in (*) do set LIST=!LIST! %i
    echo %LIST%

If Command Extensions are enabled, then there are several dynamic
environment variables that can be expanded but which don't show up in
the list of variables displayed by SET.  These variable values are
computed dynamically each time the value of the variable is expanded.
If the user explicitly defines a variable with one of these names, then
that definition will override the dynamic one described below:

%CD% - expands to the current directory string.

%DATE% - expands to current date using same format as DATE command.

%TIME% - expands to current time using same format as TIME command.

%RANDOM% - expands to a random decimal number between 0 and 32767.

%ERRORLEVEL% - expands to the current ERRORLEVEL value

%CMDEXTVERSION% - expands to the current Command Processor Extensions
    version number.

%CMDCMDLINE% - expands to the original command line that invoked the
    Command Processor.

Important information, tips for the "SET" command

There are a few important aspects to keep in mind when using the `SET` command in the Windows Command Prompt: 1. Overwriting Environment Variables: If you create an environment variable with `SET` using the same name as an existing environment variable, the existing variable will be overwritten. Make sure you intend to do this to avoid unwanted effects.

    SET EXISTING_VARIABLE=NewValue
    
2. Spaces in values: When setting values ??with spaces in environment variables, it is advisable to enclose the value in double quotes. This helps avoid problems with interpreting spaces.

    SET VARIABLE_WITH_SPACES="Value with spaces"
    
3. Display all environment variables: Simply typing `SET` without any specific variable will display all environment variables and their values. This can be helpful to ensure that your changes were made as expected.

    SET
    
4. Setting environment variables in batch scripts: When setting environment variables in a batch script, you should note that the variable will be accessed later in the script by `%Variable%`.

    SET NEW_VARIABLE=Content
    ECHO %NEW_VARIABLE%
    
5. User Input with `SET /P`: When using `SET /P` for user input, make sure to clearly prompt the user and provide clear instructions to avoid confusion.

    SET /P USER INPUT=Enter something:
    
6. Case Sensitivity: Note that environment variables are typically not case sensitive. This means that `%Variable%` and `%variable%` access the same thing. These tips are intended to ensure that you use the `SET` command effectively and safely.


Deutsch
English
Español
Français
Italiano
日本語 (Nihongo)
한국어 (Hangugeo)
汉语 (Hànyǔ)
Türkçe
Português
Português
Svenska
Norsk
Dansk
Suomi
Nederlands
Polski









Windows-10


... Windows 10 FAQ
... Windows 10 How To


Windows 10 How To


... Windows 11 How To
... Windows 10 FAQ



The command SET - Displays, sets, or removes Windows environment variables.

HTTP: ... console/en/058.htm
0.077
16208
AllowUpgradesWithUnsupportedTPMOrCPU
MS Shell directory menus and right-click function in Q-Dir!
Wird es die Windows Einstellungen ⚙️ in Windows 12 geben?
New shortcut excluding "shortcut" in the name under Windows!
Difference Between Laptop and Notebook?
The symbol spacing is very close together in Quad Explorer for Windows!
Is it possible to edit the directory path in the new address bar?
Wo finde ich unter Windows 11 die Akku-Status-Informationen!
Command prompt or the ColorConsole as admin and advantages?
Does the alternate command prompt work on Windows 11?



(0)