Bird
0
0

You want to create a string that shows the current date and time in PowerShell using interpolation. Which code correctly does this?

hard📝 Application Q8 of 15
PowerShell - Variables and Data Types
You want to create a string that shows the current date and time in PowerShell using interpolation. Which code correctly does this?
$now = Get-Date
$output = "Current time: $($now.ToString('HH:mm:ss'))"
Write-Output $output
ACurrent time: $($now)
BCurrent time: $now.ToString('HH:mm:ss')
CCurrent time: 14:30:45 (example output)
DError: Method ToString not found
Step-by-Step Solution
Solution:
  1. Step 1: Use Get-Date to get current date/time object

    $now stores the current date and time as an object.
  2. Step 2: Use subexpression $() to call ToString method with format

    $($now.ToString('HH:mm:ss')) evaluates to the time string like "14:30:45".
  3. Final Answer:

    Current time: 14:30:45 (example output) -> Option C
  4. Quick Check:

    Use $() to call methods inside strings [OK]
Quick Trick: Use $() to call methods inside strings [OK]
Common Mistakes:
  • Not using $() for method calls
  • Expecting method calls to work without subexpression
  • Confusing string literals with evaluated output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes