0
0
PowerShellscripting~20 mins

Environment variables in PowerShell - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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.