Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Environment variables
📖 Scenario: You are writing a PowerShell script to manage environment variables for a simple application setup. Environment variables store important settings like paths or configuration values that your script can use.
🎯 Goal: Build a PowerShell script that creates a new environment variable, reads an existing environment variable, and then displays the values.
📋 What You'll Learn
Create a new environment variable with a specific name and value
Read the value of an existing environment variable
Display both environment variable values using Write-Output
💡 Why This Matters
🌍 Real World
Managing environment variables is common when setting up software, configuring scripts, or customizing user environments.
💼 Career
Many IT, DevOps, and scripting jobs require working with environment variables to automate setups and deployments.
Progress0 / 4 steps
1
Create a new environment variable
Create a new environment variable called MyAppPath and set its value to C:\MyApp\bin using PowerShell syntax.
PowerShell
Hint
Use $env:VariableName = 'Value' to set environment variables in PowerShell.
2
Read an existing environment variable
Create a variable called systemPath and assign it the value of the existing environment variable PATH using PowerShell syntax.
PowerShell
Hint
Use $variable = $env:VariableName to read environment variables.
3
Display the environment variable values
Use Write-Output to display the values of MyAppPath and systemPath variables on separate lines.
PowerShell
Hint
Use Write-Output $variable to print values in PowerShell.
4
Run the script and check output
Run the script to print the values of MyAppPath and systemPath. The output should show C:\MyApp\bin on the first line and the full PATH value on the second line.
PowerShell
Hint
The first line of output must be exactly C:\MyApp\bin. The second line will be your system's PATH value.
Practice
(1/5)
1. What is the correct way to access the environment variable PATH in PowerShell?
The command $env:GREETING = 'Hello' sets the environment variable GREETING to 'Hello'.
Step 2: Output the string with variable expansion
The command Write-Output "$env:GREETING, World!" expands $env:GREETING to 'Hello', so the output is 'Hello, World!'.
Final Answer:
Hello, World! -> Option A
Quick Check:
Variable expands correctly = Hello, World! [OK]
Hint: Variables inside double quotes expand automatically in PowerShell [OK]
Common Mistakes:
Expecting literal $env:GREETING instead of expansion
Using single quotes which prevent expansion
Assuming environment variables are not accessible in strings
4. You run this script but get an error:
Write-Output $envMY_VAR
What is the error and how to fix it?
medium
A. Environment variables cannot be used in Write-Output
B. Variable name is case sensitive; use $env:my_var
C. Missing colon after env; fix by using $env:MY_VAR
D. Use double quotes around variable: "$envMY_VAR"
Solution
Step 1: Identify syntax error in variable name
The variable $envMY_VAR is invalid because environment variables require a colon after env, like $env:MY_VAR.
Step 2: Correct the syntax
Fix the script by changing to Write-Output $env:MY_VAR to properly access the environment variable.
Final Answer:
Missing colon after env; fix by using $env:MY_VAR -> Option C
Quick Check:
Env vars need colon after env = $env:VAR [OK]
Hint: Always use colon after env to access variables: $env:VAR [OK]
Common Mistakes:
Omitting colon after env
Assuming variable names are case sensitive
Trying to quote variable name instead of fixing syntax
5. You want to temporarily add a folder C:\Tools to the PATH environment variable for your current PowerShell session only. Which command correctly does this?
hard
A. $env:PATH = $env:PATH + ';C:\Tools'
B. setx PATH "$env:PATH;C:\Tools"
C. New-Item -Path Env:PATH -Value 'C:\Tools'
D. $env:PATH += 'C:\Tools'
Solution
Step 1: Understand session vs persistent environment variables
Modifying $env:PATH directly changes it only for the current session.
Step 2: Append new folder to existing PATH
Use $env:PATH = $env:PATH + ';C:\Tools' to add the folder, separating with a semicolon.
Final Answer:
$env:PATH = $env:PATH + ';C:\Tools' -> Option A
Quick Check:
Append with $env:PATH = $env:PATH + ';new_path' [OK]
Hint: Append with $env:PATH = $env:PATH + ';new_folder' for session only [OK]
Common Mistakes:
Using setx which changes persistent variables, not session