Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
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.
Environment variables in Windows can be categorized into two types:
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.
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.
You can view environment variables using the Command Prompt (CMD) or PowerShell.
To list all environment variables, open the Command Prompt and type:
set
This command will display all the environment variables and their current values.
To list all environment variables in PowerShell, use the following command:
Get-ChildItem Env:
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.
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.
To delete an environment variable for the current session, use:
set MY_VARIABLE=
For a permanent deletion, use:
setx MY_VARIABLE ""
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")
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"
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%