A system administrator is writing a Windows PowerShell script (`.ps1`) to automate user environment auditing. The script needs to retrieve the built-in system environment variable for the logged-in username and display it to the console. Which of the following code snippets uses the correct syntax to reference an environment variable in PowerShell?
- Write-Output $env:USERNAMEAnswer
- BWrite-Output %USERNAME%
- CWrite-Output $USER
- DWrite-Output %env:USERNAME%
Answer
The command line using 'Write-Output $env:USERNAME' correctly references an environment variable in PowerShell.
In Windows PowerShell, system environment variables are accessed via the environment drive provider using the ' env:USERNAME).
Step-by-Step Solution
Key Concept
PowerShell Environment Variable Syntax