Bird
Raised Fist0
PowerShellscripting~20 mins

Here-strings for multiline in PowerShell - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Here-strings 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 here-string?
Consider the following PowerShell script using a here-string. What will be the exact output when this script runs?
PowerShell
$text = @"
Hello,
This is a multiline
string.
"@
Write-Output $text
A
Hello,
This is a multiline
string.
BHello, This is a multiline string.
C"Hello,\nThis is a multiline\nstring."
DHello,This is a multilinestring.
Attempts:
2 left
💡 Hint
Remember that here-strings preserve line breaks exactly as typed.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a here-string in PowerShell?
Select the option that correctly creates a multiline string using a here-string in PowerShell.
A
$str = @"
Line 1
Line 2
'
B
$str = @"
Line 1
Line 2
"@
C
$str = @'
Line 1
Line 2
"@
D
$str = '@
Line 1
Line 2
@'
Attempts:
2 left
💡 Hint
Here-strings start with @" and end with "@ or start with @' and end with '@ but must match.
🔧 Debug
advanced
2:00remaining
Why does this here-string cause an error?
This PowerShell code throws an error. What is the cause?
PowerShell
$text = @"
This is a test
  "@
Write-Output $text
AThe closing delimiter "@" must be alone on its line with no spaces.
BHere-strings cannot contain the word 'test'.
CThe opening delimiter @" is missing a closing quote.
DWrite-Output cannot print here-strings.
Attempts:
2 left
💡 Hint
Check how the closing delimiter is placed in the code.
🚀 Application
advanced
2:00remaining
How to include variables inside a here-string?
Given $name = "Alice", which here-string will correctly include the variable's value in the output?
PowerShell
$name = "Alice"
$text = @?
Hello, $name!
Welcome.
?@
Write-Output $text
A
$text = @'
Hello, " + $name + "!
Welcome.
'@
B
$text = @'
Hello, $name!
Welcome.
'@
C
$text = @"
Hello, '$name'!
Welcome.
"@
D
$text = @"
Hello, $name!
Welcome.
"@
Attempts:
2 left
💡 Hint
Double-quoted here-strings allow variable expansion, single-quoted do not.
🧠 Conceptual
expert
2:00remaining
What is the number of lines in the output of this here-string?
How many lines will the following PowerShell script output?
PowerShell
$text = @"
Line1

Line3
Line4
"@
Write-Output $text | Measure-Object -Line
A4
B3
C5
D6
Attempts:
2 left
💡 Hint
Count all lines including empty ones preserved by the here-string.

Practice

(1/5)
1. What is the main purpose of using a here-string in PowerShell?
easy
A. To execute multiple commands in one line
B. To create multiline strings preserving formatting and line breaks
C. To define a function with multiple parameters
D. To comment out multiple lines of code

Solution

  1. Step 1: Understand what a here-string is

    A here-string is a way to write text that spans multiple lines without losing formatting or line breaks.
  2. Step 2: Compare options with the definition

    Only To create multiline strings preserving formatting and line breaks correctly describes the purpose of here-strings in PowerShell.
  3. Final Answer:

    To create multiline strings preserving formatting and line breaks -> Option B
  4. Quick Check:

    Here-strings = multiline text [OK]
Hint: Here-strings keep text exactly as typed across lines [OK]
Common Mistakes:
  • Confusing here-strings with command execution
  • Thinking here-strings are for comments
  • Assuming here-strings define functions
2. Which of the following is the correct syntax to start a double-quoted here-string in PowerShell?
easy
A. @' (at sign followed by single quote)
B. "@ (double quote followed by at sign)
C. @ (at sign followed by double quote)
D. @" (at sign followed by double quote)

Solution

  1. Step 1: Recall here-string syntax

    Double-quoted here-strings start with @" and end with "@.
  2. Step 2: Match options with syntax

    @" (at sign followed by double quote) correctly shows @" as the start of a double-quoted here-string.
  3. Final Answer:

    @" -> Option D
  4. Quick Check:

    Double-quoted here-string start = @" [OK]
Hint: Double-quoted here-strings start with @" [OK]
Common Mistakes:
  • Using single quote instead of double quote for double-quoted here-string
  • Reversing the order of @ and "
  • Confusing start and end delimiters
3. What will be the output of this PowerShell code?
$name = "Alice"
$text = @"
Hello, $name!
Welcome to PowerShell.
"@
Write-Output $text
medium
A. Hello, Alice! Welcome to PowerShell.
B. Hello, $name! Welcome to PowerShell.
C. Hello, Alice! Welcome to PowerShell.
D. Hello, $name! Welcome to PowerShell.

Solution

  1. Step 1: Identify the here-string type

    The here-string uses @" ... "@ which is double-quoted, so variables inside are expanded.
  2. Step 2: Evaluate variable expansion

    The variable $name is replaced by its value "Alice" inside the here-string.
  3. Final Answer:

    Hello, Alice! Welcome to PowerShell. -> Option A
  4. Quick Check:

    Double-quoted here-string expands variables [OK]
Hint: Double quotes expand variables inside here-strings [OK]
Common Mistakes:
  • Assuming variables are not expanded in double-quoted here-strings
  • Ignoring line breaks in output
  • Confusing single and double quotes for here-strings
4. Identify the error in this PowerShell script:
$text = @'
Line 1
Line 2
"@
Write-Output $text
medium
A. The here-string is not closed properly with '@
B. Using single quotes disables variable expansion
C. The ending delimiter "@ is incorrect for single-quoted here-string
D. Missing Write-Output command

Solution

  1. Step 1: Check the here-string delimiters

    The here-string starts with @' (single-quoted) but ends with "@ (double-quoted), which is incorrect.
  2. Step 2: Correct delimiter matching

    Single-quoted here-strings must end with '@, matching the start delimiter.
  3. Final Answer:

    The ending delimiter "@ is incorrect for single-quoted here-string -> Option C
  4. Quick Check:

    Start and end delimiters must match [OK]
Hint: Match start and end delimiters exactly for here-strings [OK]
Common Mistakes:
  • Mixing single and double quotes in delimiters
  • Forgetting to close the here-string
  • Assuming variable expansion error causes syntax error
5. You want to create a multiline string in PowerShell that includes the literal text "$path" without expanding it, but also includes a variable $user expanded. Which here-string syntax should you use?
hard
A. Use double-quoted here-string @" ... "@ and escape $path as `$path
B. Use single-quoted here-string @' ... '@ and write $user outside it
C. Use double-quoted here-string @" ... "@ and write $path as is
D. Use single-quoted here-string @' ... '@ and include $user inside it

Solution

  1. Step 1: Understand variable expansion in here-strings

    Double-quoted here-strings expand variables, single-quoted do not.
  2. Step 2: Requirement analysis

    You want $user expanded but $path literal. So use double-quoted here-string and escape $path as `$path to prevent expansion.
  3. Final Answer:

    Use double-quoted here-string @" ... "@ and escape $path as `$path -> Option A
  4. Quick Check:

    Escape variables to keep literal in double-quoted here-strings [OK]
Hint: Escape $ with backtick to keep literal in double-quoted here-strings [OK]
Common Mistakes:
  • Using single-quoted here-string disables all variable expansion
  • Not escaping $path causes unwanted expansion
  • Trying to expand variables inside single-quoted here-string
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard