0
0
PowerShellscripting~10 mins

Why best practices improve reliability in PowerShell - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to display a message about script reliability.

PowerShell
Write-Output [1]
Drag options to blanks, or click blank then click option'
AWrite-Output "Scripts are unreliable."
B"Scripts that follow best practices are more reliable."
CScripts that follow best practices are more reliable.
Decho Scripts are unreliable
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put the message inside quotes causes errors.
Using echo without quotes outputs unexpected results.
2fill in blank
medium

Complete the code to check if a file exists before reading it.

PowerShell
if (Test-Path [1]) { Get-Content [1] } else { Write-Output "File not found." }
Drag options to blanks, or click blank then click option'
A"C:\data.txt"
B'data.txt'
Cdata.txt
DC:\data.txt
Attempts:
3 left
💡 Hint
Common Mistakes
Not quoting the file path causes errors.
Using single quotes with backslashes can cause escape issues.
3fill in blank
hard

Fix the error in the script that tries to create a directory if it doesn't exist.

PowerShell
if (-Not (Test-Path [1])) { New-Item -ItemType Directory -Path [1] }
Drag options to blanks, or click blank then click option'
A'C:\Logs'
BC:\Logs
C"C:\Logs"
DLogs
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted paths causes command failures.
Using single quotes with backslashes can cause unexpected behavior.
4fill in blank
hard

Fill both blanks to create a function that logs messages with timestamps.

PowerShell
function Log-Message {
    param([string]$message)
    $timestamp = Get-Date -Format [1]
    Write-Output "$timestamp - [2]"
}
Drag options to blanks, or click blank then click option'
A"yyyy-MM-dd HH:mm:ss"
B$message
Cmessage
D"MM/dd/yyyy"
Attempts:
3 left
💡 Hint
Common Mistakes
Not quoting the date format string causes errors.
Using 'message' without $ does not insert the variable value.
5fill in blank
hard

Fill all three blanks to filter services that are running and display their names.

PowerShell
$services = Get-Service | Where-Object { $_.Status -eq [1] }
$names = $services | Select-Object -ExpandProperty [2]
$names | ForEach-Object { Write-Output [3] }
Drag options to blanks, or click blank then click option'
A"Running"
BName
C$_
DStatus
Attempts:
3 left
💡 Hint
Common Mistakes
Not quoting 'Running' causes filter failure.
Selecting wrong property or not expanding it causes errors.
Using wrong variable in ForEach-Object outputs nothing.