Top 10 PowerShell scenario questions
Try each one yourself first, then open the model answer.
1. A try/catch does not catch a failing cmdlet. Why?
Show answerHide answer
Many errors are non-terminating. Add -ErrorAction Stop or set $ErrorActionPreference to Stop in the script.
2. Filtering after retrieving every user is slow. What do you change?
Show answerHide answer
Filter at the source with the cmdlet's -Filter parameter rather than Where-Object afterwards.
3. A script that deletes accounts must be safe to test. How?
Show answerHide answer
Support -WhatIf and -Confirm with SupportsShouldProcess, log actions and test on a small group first.
4. A function returns extra unexpected values. What is the cause?
Show answerHide answer
Any uncaptured output goes to the pipeline. Assign to $null or cast to [void] for output you do not want.
5. Credentials are hard-coded in a script. What is better?
Show answerHide answer
Use a secret store, managed identities or Get-Credential for interactive use, and never commit secrets.
6. You need to run the same task on 200 servers. How?
Show answerHide answer
Use Invoke-Command with several computers, control concurrency with ThrottleLimit and collect errors per machine.
7. Comparing a collection with -eq returns items instead of true or false. Why?
Show answerHide answer
With a collection on the left, comparison operators filter it. Use -contains or -in for membership tests.
8. A CSV export shows only type names for a property. What happened?
Show answerHide answer
The property holds an object or array. Select a calculated property that turns it into text.
9. A script works in PowerShell 7 but not Windows PowerShell 5.1. What do you check?
Show answerHide answer
Newer operators and parameters, default encodings and module compatibility. Declare the required version with #Requires.
10. How do you make a scheduled script easy to troubleshoot?
Show answerHide answer
Use a transcript or structured logging, return meaningful exit codes and alert on failure.
