A systems administrator is updating a legacy Windows batch script (.bat) used during user logon to map a personalized directory path on a network share. The script needs to reference the standard environment variable that dynamically retrieves the currently logged-in account name. Which of the following syntax formats correctly accesses this environment variable inside a Windows batch script?
- %USERNAME%Answer
- B$env:USERNAME
- C$USERNAME
- D/USERNAME
Answer
The syntax `%USERNAME%` correctly references the environment variable in a Windows batch script.
In Windows Command Prompt batch scripts (.bat), environment variables are evaluated by surrounding the variable name with percent signs, such as `%USERNAME%`. When executed, `cmd.exe` replaces `%USERNAME%` with the current user's logon name.
Step-by-Step Solution
Key Concept
Windows Batch Script Environment Variable Syntax