Text preview & study summary

PowerShell Scripting Basics

A free sample of 5 questions from this quiz, shown in full with answer choices and explanations. No interactivity — everything is visible on this page for study and review.

Want to test your knowledge? Launch the Interactive Exam Simulator

Question 1

PowerShell cmdlets pass objects through the pipeline, unlike traditional shells that pass plain text strings.

Answer choices

  • A. True (Correct)

  • B. False

Explanation

This is one of PowerShell's most powerful features and key differentiators. When you pipe in bash: `ls | grep foo` — grep receives plain text. In PowerShell: `Get-Process | Where-Object CPU -gt 10` — Where-Object receives full .NET Process objects with all properties (CPU, Memory, PID, Name, etc.). The receiving cmdlet can filter, sort, and manipulate these objects using their properties. This enables: `Get-Service | Where-Object {$_.Status -eq "Running"} | Stop-Service` — each command works with structured objects. `$_` (or `$PSItem`) is the current pipeline object.

Question 2

Which PowerShell operator is used for string interpolation within double-quoted strings?

Answer choices

  • A. + (concatenation)

  • B. $ followed by a variable name directly within the string (Correct)

  • C. & (ampersand operator)

  • D. % (format operator)

Explanation

PowerShell string types: Double-quoted strings `"..."` — variables and escape sequences are expanded: `$name = "World"; "Hello $name"` → "Hello World". For expressions: `"Today is $(Get-Date -Format 'MM/dd/yyyy')"` — use `$()` subexpression. Single-quoted strings `'...'` — literal strings, NO expansion: `'Hello $name'` → "Hello $name" (literal dollar sign). Escape character is backtick (`) not backslash: `` `n `` = newline, `` `t `` = tab, `` `" `` = literal quote. String concatenation: `"Hello" + " " + "World"` also works but direct interpolation is more readable and idiomatic PowerShell.

Question 3

In PowerShell, `try { } [[blank1]] { } [[blank2]] { }` is the error handling structure where the middle block catches exceptions and the last block always executes regardless of whether an error occurred.

Explanation

PowerShell error handling structure:

```powershell

try {

# Code that might throw an error

Get-Content "C:\nonexistent.txt" -ErrorAction Stop

} catch [System.IO.FileNotFoundException] {

Write-Error "File not found: $_"

} catch {

Write-Error "Unexpected error: $($_.Exception.Message)"

} finally {

Write-Host "This always runs — cleanup code here"

}

```

`-ErrorAction Stop` converts non-terminating errors to terminating errors (catchable). `$_` in catch block is the error record. Multiple catch blocks handle different exception types. `finally` runs even if an exception occurs — used for cleanup (closing files, connections). `$Error[0]` holds the most recent error.

Question 4

The `$PSVersionTable` variable in PowerShell contains information about the current PowerShell version and can be used to write version-compatible scripts.

Answer choices

  • A. True (Correct)

  • B. False

Explanation

`$PSVersionTable` is an automatic read-only hashtable containing: `PSVersion` (PowerShell version), `PSEdition` (Desktop for Windows PowerShell 5.x; Core for PowerShell 6+), `PSCompatibleVersions`, `BuildVersion`, `CLRVersion`, `WSManStackVersion`. Version checking in scripts: `if ($PSVersionTable.PSVersion.Major -ge 7) { # PS7 features }`. PowerShell 5.1 (Windows PowerShell) ships with Windows; PowerShell 7+ (Core) is cross-platform (Windows/Linux/macOS), open source, and has new features (ForEach-Object -Parallel, null coalescing ??). Scripts targeting both should handle version differences gracefully.

Question 5

The PowerShell cmdlet [[blank1]] converts output to HTML format, while [[blank2]] converts to CSV format, enabling easy export of data for reports and data sharing.

Explanation

PowerShell output formatting cmdlets: `ConvertTo-Html` — generates an HTML table from objects, supports -Head (CSS styles), -Title, -PreContent, -PostContent; pipe to `Out-File report.html` for file output. `Export-Csv` — creates a CSV file directly (more common); `-NoTypeInformation` removes header comment; `Import-Csv` reads CSV back as objects. `ConvertTo-Csv` outputs CSV strings to pipeline. `ConvertTo-Json` / `ConvertFrom-Json` — JSON conversion (API integration). `ConvertTo-Xml` — XML output. `Out-GridView` — interactive filterable GUI table (great for exploration). These enable PowerShell to serve as a powerful reporting and data export tool.