Question

Difficulty: MediumBasic Scripting Languages and Constructs

A desktop support technician is writing a Windows Batch script (`.bat`) to automate system profile configuration during user logon. The script must reference the currently logged-in user's account name dynamically from the system environment. Which of the following syntaxes correctly formats an environment variable within a Windows Batch script?

  1. %USERNAME%Answer
  2. B
    $USERNAME
  3. C
    $env:USERNAME
  4. D
    var USERNAME

Answer

Enclosing the variable name between percent signs (%USERNAME%) is the correct construct for environment variables in Windows Batch scripts.
In Windows Batch (`.bat`) scripting, environment variables are referenced by surrounding the variable name with percent signs, such as %USERNAME%. When the batch file executes, the Command Prompt interpreter replaces the expression with the actual environment variable value.

Step-by-Step Solution

1
Identify the target scripting language and objective from the scenario.
The target language is Windows Batch (`.bat`) and the goal is to reference a system environment variable.
Different scripting environments utilize specific syntax rules for variable declaration and retrieval.
2
Evaluate the syntax rule for environment variables in Windows Command Prompt / Batch.
Windows Batch environment variables are delimited using percent signs surrounding the variable name (e.g., %VARIABLE_NAME%).
The command processor recognizes percent signs as delimiters to replace the variable placeholder with its runtime value.

Key Concept

Windows Batch Environment Variable Syntax
Estimated Time:1m 0s
Rate this question