Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Work with Environment Variables in Windows

Environment variables are a set of dynamic values that can affect the way running processes will behave on a computer. They are part of the operating system environment and are used by the operating system and various programs to determine where to store temporary files, where to find user profiles, and other configuration settings. Windows provides several ways to manage these variables, and understanding how to work with them can enhance your system management and scripting capabilities.

Understanding Environment Variables in Windows

Environment variables in Windows can be categorized into two types:

  1. System Variables: These are defined by the operating system and apply to all users. They are typically set during the installation of the operating system or applications.

  2. User Variables: These are specific to the user account and can be customized by the user. They are often used to store user-specific settings.

How to View Environment Variables

You can view environment variables using the Command Prompt (CMD) or PowerShell.

Using Command Prompt

To list all environment variables, open the Command Prompt and type:

set

This command will display all the environment variables and their current values.

Using PowerShell

To list all environment variables in PowerShell, use the following command:

Get-ChildItem Env:

How to Create or Modify Environment Variables

Using Command Prompt

To create or modify an environment variable for the current session in CMD, use the set command:

set MY_VARIABLE=MyValue

This sets a variable named MY_VARIABLE with the value MyValue for the current session. Note that this change is temporary and will be lost when the session ends.

To set a permanent environment variable, use the setx command:

setx MY_VARIABLE "MyValue"

This will create or modify the MY_VARIABLE environment variable and make it persistent across sessions.

Using PowerShell

To set an environment variable in PowerShell for the current session, use:

$env:MY_VARIABLE = "MyValue"

For a permanent change, you can use the [System.Environment]::SetEnvironmentVariable method:

[System.Environment]::SetEnvironmentVariable("MY_VARIABLE", "MyValue", "User")

Replace "User" with "Machine" if you want to set the system-wide variable.

How to Delete Environment Variables

Using Command Prompt

To delete an environment variable for the current session, use:

set MY_VARIABLE=

For a permanent deletion, use:

setx MY_VARIABLE ""

Using PowerShell

To remove an environment variable in PowerShell for the current session, use:

Remove-Item Env:MY_VARIABLE

For a permanent deletion, use:

[System.Environment]::SetEnvironmentVariable("MY_VARIABLE", $null, "User")

Practical Examples

  1. Add a Path to the PATH Variable

    Adding a directory to the PATH environment variable allows you to run executables located in that directory without specifying the full path. To add a directory to the PATH variable for the current session in CMD:

    set PATH=%PATH%;C:\MyDirectory

    For a permanent change:

    setx PATH "%PATH%;C:\MyDirectory"
  2. Create a Custom Variable

    To create a custom environment variable named MY_APP_CONFIG with a value of C:\Config:

    setx MY_APP_CONFIG "C:\Config"

    Verify by typing:

    echo %MY_APP_CONFIG%

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.