Bird
Raised Fist0
PowerShellscripting~20 mins

Environment variables in PowerShell - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
Environment Variable Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this PowerShell command?
Consider the following command executed in PowerShell. What will it output?
PowerShell
Write-Output $env:USERNAME
AThe literal string '$env:USERNAME'
BAn empty string
CThe current user's username as a string, e.g., 'Alice'
DA runtime error indicating variable not found
Attempts:
2 left
💡 Hint
Environment variables in PowerShell are accessed with $env: prefix.
📝 Syntax
intermediate
2:00remaining
Which command correctly sets a new environment variable in PowerShell for the current session?
You want to set an environment variable named 'MY_VAR' with value 'HelloWorld' for the current PowerShell session. Which command is correct?
Aset-env MY_VAR 'HelloWorld'
B$env:MY_VAR = 'HelloWorld'
Cexport MY_VAR='HelloWorld'
Denv MY_VAR = 'HelloWorld'
Attempts:
2 left
💡 Hint
PowerShell uses $env: prefix to set environment variables.
🔧 Debug
advanced
2:00remaining
Why does this script fail to update the environment variable permanently?
This script tries to set an environment variable permanently but it doesn't work as expected. Why? $env:PATH = "$env:PATH;C:\NewFolder"
PowerShell
$env:PATH = "$env:PATH;C:\NewFolder"
ABecause the syntax for appending to PATH is incorrect and causes a syntax error
BBecause the path separator should be a colon ':' instead of a semicolon ';'
CBecause environment variables cannot be changed in PowerShell
DBecause $env:PATH only changes the variable for the current session, not permanently
Attempts:
2 left
💡 Hint
Think about session scope vs permanent environment variables.
🚀 Application
advanced
2:00remaining
How to retrieve and display all environment variables in PowerShell?
You want to list all environment variables and their values in PowerShell. Which command will do this?
AGet-ChildItem Env:
BGet-EnvVariables
Cls $env:
Ddir $env:USERNAME
Attempts:
2 left
💡 Hint
Environment variables are stored in a special drive in PowerShell.
🧠 Conceptual
expert
2:00remaining
What happens when you run this PowerShell command?
What is the output or effect of running this command? [Environment]::SetEnvironmentVariable('TEST_VAR', '123', 'User') Write-Output $env:TEST_VAR
PowerShell
[Environment]::SetEnvironmentVariable('TEST_VAR', '123', 'User')
Write-Output $env:TEST_VAR
AOutputs nothing because the variable is set permanently but not updated in the current session
BOutputs the previous value of TEST_VAR before the change
CThrows a runtime error because SetEnvironmentVariable is not valid in PowerShell
DOutputs '123' because the variable is immediately available in the session
Attempts:
2 left
💡 Hint
Think about session environment variables vs permanent environment variables.

Practice

(1/5)
1. What is the correct way to access the environment variable PATH in PowerShell?
easy
A. Use PATH$
B. Use get-env PATH
C. Use env(PATH)
D. Use $env:PATH

Solution

  1. Step 1: Understand PowerShell environment variable syntax

    PowerShell uses $env:VARIABLE_NAME to access environment variables.
  2. Step 2: Apply syntax to PATH variable

    To get the PATH variable, write $env:PATH.
  3. Final Answer:

    Use $env:PATH -> Option D
  4. Quick Check:

    Environment variable access = $env:VARIABLE [OK]
Hint: Remember: environment variables use $env: prefix in PowerShell [OK]
Common Mistakes:
  • Using incorrect syntax like get-env or env()
  • Forgetting the $env: prefix
  • Trying to access variables without colon
2. Which of the following is the correct syntax to set an environment variable named MY_VAR to the value hello in PowerShell?
easy
A. set-env MY_VAR hello
B. $env:MY_VAR = 'hello'
C. env MY_VAR = hello
D. set $env:MY_VAR hello

Solution

  1. Step 1: Recall how to assign environment variables in PowerShell

    PowerShell assigns environment variables by setting $env:VariableName to a value.
  2. Step 2: Apply assignment to MY_VAR

    Use $env:MY_VAR = 'hello' to set the variable.
  3. Final Answer:

    $env:MY_VAR = 'hello' -> Option B
  4. Quick Check:

    Set env var = $env:VAR = value [OK]
Hint: Set env vars with $env:VAR = 'value' syntax [OK]
Common Mistakes:
  • Using set-env which is not a PowerShell cmdlet
  • Missing quotes around string values
  • Trying to assign without $env: prefix
3. What will be the output of the following PowerShell commands?
$env:GREETING = 'Hello'
Write-Output "$env:GREETING, World!"
medium
A. Hello, World!
B. $env:GREETING, World!
C. GREETING, World!
D. Error: Variable not found

Solution

  1. Step 1: Assign environment variable GREETING

    The command $env:GREETING = 'Hello' sets the environment variable GREETING to 'Hello'.
  2. 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!'.
  3. Final Answer:

    Hello, World! -> Option A
  4. 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

  1. 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.
  2. Step 2: Correct the syntax

    Fix the script by changing to Write-Output $env:MY_VAR to properly access the environment variable.
  3. Final Answer:

    Missing colon after env; fix by using $env:MY_VAR -> Option C
  4. 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

  1. Step 1: Understand session vs persistent environment variables

    Modifying $env:PATH directly changes it only for the current session.
  2. Step 2: Append new folder to existing PATH

    Use $env:PATH = $env:PATH + ';C:\Tools' to add the folder, separating with a semicolon.
  3. Final Answer:

    $env:PATH = $env:PATH + ';C:\Tools' -> Option A
  4. 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
  • Using += without semicolon separator
  • Overwriting PATH without appending