Bird
0
0

You want to create a file path string in PowerShell that includes a variable folder name. Which code correctly uses string interpolation to build the path?

hard📝 Application Q15 of 15
PowerShell - String Operations
You want to create a file path string in PowerShell that includes a variable folder name. Which code correctly uses string interpolation to build the path?
$folder = "Documents"
$path = ?
A"C:/Users$folder/file.txt"
B'C:\Users\$folder\file.txt'
C"C:\Users\$folder\file.txt"
D'C:/Users/$folder/file.txt'
Step-by-Step Solution
Solution:
  1. Step 1: Understand string interpolation and path separators

    Double quotes allow $folder to be replaced by its value. PowerShell treats backslashes as literal characters in double-quoted strings and uses them for Windows paths.
  2. Step 2: Check each option

    "C:/Users$folder/file.txt": "C:/Users$folder/file.txt" interpolates but misses separator ("C:/UsersDocuments/file.txt"), invalid path.
    'C:\Users\$folder\file.txt': 'C:\Users\$folder\file.txt' uses single quotes, no interpolation.
    "C:\Users\$folder\file.txt": "C:\Users\$folder\file.txt" uses double quotes and proper Windows path syntax, interpolates to valid path.
    'C:/Users/$folder/file.txt': uses single quotes, no interpolation.
  3. Final Answer:

    "C:\Users\$folder\file.txt" -> Option C
  4. Quick Check:

    Double quotes + Windows path backslashes + $variable = valid interpolated path [OK]
Quick Trick: Use double quotes and backslashes for Windows paths with variables [OK]
Common Mistakes:
  • Using single quotes which block interpolation
  • Missing path separators between directories and variables
  • Using forward slashes instead of backslashes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes