Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 34 ยท Practice and Review

PowerShell Interview Questions

26 PowerShell interview questions with short answers: objects, the pipeline, errors, scope, functions, remoting, jobs and security, with two runnable stand-ins.

PowerShell interviews are rarely about memorizing cmdlet names. A good interviewer wants to know whether you understand the ideas behind the shell: why it passes objects instead of text, what "terminating error" means, why a variable changes in one place and not another, and what is dangerous about running scripts at scale. This page collects 26 questions that come up again and again, with short, accurate answers you can expand in your own words.

Try to answer each question out loud before you open it. If you cannot explain an idea in two or three sentences, follow the link and review the lesson. PowerShell cannot run on this site, so the real code in the answers is read-only. Two stand-ins in JavaScript let you experiment with the trickiest topics: variable scope and the comparison operators. They model the rules, not the real engine.

What interviewers listen for

Objects Properties and methods, not text scraping.
Pipeline Streaming, binding, formatting last.
Errors Terminating or not, and what you do.
Safety -WhatIf, least privilege, no secrets in code.

A short answer plus one trade-off beats a long recital.

1 / 2

When you do not know an answer, say how you would find out: Get-Help, Get-Member, Get-Command. In a real job that is exactly what you do, and interviewers know it. See Getting Help.

Fundamentals

1. What is PowerShell, and how is it different from cmd or Bash?

PowerShell is a shell and scripting language built on .NET. Its commands (cmdlets) pass objects with typed properties and methods through the pipeline, while cmd and Bash pass lines of text between programs. That removes most text parsing: you filter on $_.Status, not on a column found with awk. It is also a full language with functions, classes, error handling and modules. See What Is PowerShell?.

2. What is the difference between Windows PowerShell 5.1 and PowerShell 7?

Windows PowerShell 5.1 is built on the .NET Framework, ships with Windows, runs only on Windows and receives only security and critical fixes (the program is powershell.exe). PowerShell 7 is built on modern .NET, is open source and cross-platform (Windows, Linux, macOS), installs side by side and keeps evolving (the program is pwsh). Some Windows-only modules work only in 5.1 or through a compatibility layer, so check $PSVersionTable before you rely on a feature.

3. What is a cmdlet, and why Verb-Noun?

A cmdlet is a command implemented as a .NET class (or, for functions, written in PowerShell) that takes parameters and emits objects. The Verb-Noun naming (Get-Process, Set-Content) makes commands guessable: you list the approved verbs with Get-Verb and discover commands with Get-Command -Noun Service. Consistent parameter names (-Path, -Name, -Force, -WhatIf) come with the pattern. See Cmdlets and the Verb-Noun Pattern.

4. How do you learn what a command or an object can do?

Get-Help <cmdlet> -Examples for usage, Get-Command to find commands, and Get-Member to see an object's properties and methods (Get-Process | Get-Member). Help files are updated with Update-Help, and about_ topics explain concepts such as about_Scopes. Show-Command opens a small form for a cmdlet's parameters.

5. What are aliases, and should you use them in scripts?

An alias is a short name for a command (gci for Get-ChildItem, % for ForEach-Object, ? for Where-Object). They are fine at the prompt and wrong in scripts shared with others: they are harder to read, and aliases can differ between platforms and be redefined. Spell out full cmdlet names and named parameters in scripts and let PSScriptAnalyzer warn you.

Objects and the pipeline

6. How does the pipeline work?

The pipeline connects commands with |. Each command emits objects one at a time, and the next command receives them as they arrive (streaming), so the first results can appear before the first command finishes. PowerShell binds each incoming object to a parameter of the next command: by value (matching the type) or by property name. See The Pipeline.

7. Why should Format-Table or Format-List be the last step in a pipeline?

Format-* cmdlets do not emit your original objects. They emit special formatting objects that exist only to be rendered on screen. Anything after them (Export-Csv, Where-Object, Sort-Object) receives those formatting objects, not your data. Filter, sort and select first, format last, and use Select-Object or Export-Csv when you need data. See Formatting and Output.

8. What is the difference between Select-Object and Where-Object?

Where-Object filters rows: it passes through the objects for which a condition is true. Select-Object chooses columns (and can add calculated ones), takes the first or last N objects, or returns unique ones. A typical pipeline uses both: Get-Service | Where-Object Status -eq Running | Select-Object Name, DisplayName. See Where-Object, Select-Object and Sort-Object.

9. What is a calculated property?

A hashtable with a name and a script block that defines a new property in Select-Object, Sort-Object, Group-Object and Format-*: Select-Object Name, @{ Name = 'SizeMB'; Expression = { $_.Length / 1MB } }. Inside the block, $_ (or $PSItem) is the current object. It is the main way to reshape objects without writing a loop.

10. What is the difference between ForEach-Object and the foreach statement?

ForEach-Object is a cmdlet that sits in a pipeline and processes objects as they stream in, using $_. The foreach statement loops over a collection that is already in memory, using a named variable. The statement is usually faster for in-memory data, while ForEach-Object keeps memory low for large streams and composes with other cmdlets. See Group-Object, Measure-Object and ForEach-Object.

11. Why does += on an array get slow?

Arrays have a fixed size, so $a += $x creates a new array one element larger and copies everything into it. Doing that in a loop is quadratic. Use a System.Collections.Generic.List[T] and its Add() method, or let the pipeline collect results for you: $result = foreach ($i in 1..1000) { $i * 2 }. See Arrays and Hashtables.

Language and operators

12. What is the difference between single and double quotes?

Single-quoted strings are literal. Double-quoted strings expand variables ("Hi $name") and subexpressions ("Total: $($a + $b)"), and understand the backtick escape such as `n. Use single quotes by default (regex patterns, paths) and double quotes when you need expansion. Here-strings (@' ... '@ and @" ... "@) do the same over several lines. See Strings, Quoting and Here-Strings.

13. How do the comparison operators behave?

They are words, not symbols: -eq, -ne, -gt, -lt, -like (wildcards), -match (regex), -contains, -in. They are case-insensitive by default; prefix with c (-ceq) for case-sensitive or i to be explicit. The type of the left operand decides how the right one is converted, so '5' -eq 5 is true (the 5 becomes text), 5 -eq '5.0' is true (the text becomes a number), but '5.0' -eq 5 is false. See Operators and Comparison.

14. What happens when the left side of -eq is an array?

The operator becomes a filter: it returns the elements that match, not a Boolean. @(1,2,3,2) -eq 2 returns 2, 2. That is why if ($list -eq $null) is a classic bug, and why the safe style is $null -eq $value, with $null on the left. Use -contains or -in to ask "is this value in the collection?". The second stand-in below lets you try the rules.

15. What counts as true or false in an if statement?

$false, $null, 0, the empty string and an empty array are false. A non-empty string (even 'False'), a non-zero number and an array with more than one element are true. An array with exactly one element is judged by that element, which makes if (@($false)) false. When the exact value matters, compare explicitly: if ($x -eq $true) or if ($null -ne $x).

16. What is splatting?

Collecting parameters in a hashtable (or array) and passing it with @ instead of $: $params = @{ Path = 'C:\logs'; Recurse = $true; Filter = '*.log' }; Get-ChildItem @params. It keeps long commands readable, lets you build parameters conditionally and reuse them. See Arrays and Hashtables.

Functions, scripts and modules

17. What does a function return?

Everything it writes to the output stream, not just the value after return. Any expression statement that produces a value ($list.Add($x) on an ArrayList, $hashSet.Add($x)) leaks into the result. return $x simply emits $x and exits. Discard unwanted output with [void], $null = or | Out-Null. See Functions and Parameters.

18. What does [CmdletBinding()] give you?

It turns a function into an advanced function: you get the common parameters (-Verbose, -Debug, -ErrorAction, -ErrorVariable, -WhatIf and -Confirm when you declare SupportsShouldProcess), strict parameter binding that rejects unknown parameters, and access to $PSCmdlet. Combine it with [Parameter(Mandatory)], validation attributes, and begin, process and end blocks for pipeline input. See Advanced Functions.

19. Explain begin, process and end blocks.

begin runs once before any pipeline input arrives (set up connections, counters). process runs once for each input object. end runs once after the last one (totals, clean-up). A function without these blocks runs its body as an end block, so it sees only the last pipeline value.

20. How does variable scope work?

PowerShell scopes are dynamic: a child scope (a function, a script block) can read variables from its parent, and from whoever called it, but an assignment creates a new local variable that hides the parent's. The parent's value is unchanged. Use $script:name or $global:name to write to those scopes explicitly, and $using:name to read a local variable inside a remote command or job. Dot-sourcing (. .\file.ps1) runs a script in the current scope. Try it in the first stand-in below.

21. What is the execution policy, and is it a security feature?

It decides which scripts the PowerShell engine will load (Restricted, AllSigned, RemoteSigned, Bypass, and so on). It is a safety net against accidents, not a security boundary: anyone who can run PowerShell can pass code to it another way, such as piping text or using -EncodedCommand. Real protection comes from permissions, application control, constrained language mode and logging. On Linux and macOS the policy is not enforced. See Scripts and Execution Policy.

22. What is a module, and how do you find and install one?

A module packages functions, cmdlets and data in a folder (a .psm1 file plus an optional .psd1 manifest). PowerShell finds modules in the folders listed in $env:PSModulePath and auto-imports them when you call one of their commands. Get-Module -ListAvailable lists what is installed; Install-Module (PowerShellGet) or Install-PSResource (PSResourceGet) installs from the PowerShell Gallery. See Modules and Profiles.

Errors and files

23. What is the difference between terminating and non-terminating errors?

A terminating error stops the current statement (and the script, unless caught) and can be handled with try/catch. A non-terminating error is written to the error stream and the command continues with the next object, so catch does not run. Most cmdlet errors (a missing file in Get-Content) are non-terminating; throw and many .NET exceptions are terminating. -ErrorAction Stop or $ErrorActionPreference = 'Stop' converts non-terminating errors to terminating ones. See Errors, try/catch and ErrorAction.

24. How do you handle errors from native programs like git or ping?

External programs do not throw PowerShell errors. They set an exit code, which you read from $LASTEXITCODE (and $? tells you whether the last command succeeded). Check it after every call that matters: if ($LASTEXITCODE -ne 0) { throw "git failed" }. PowerShell 7.4 added the $PSNativeCommandUseErrorActionPreference preference variable to turn non-zero exit codes into errors automatically.

Remoting, jobs and security

25. How does PowerShell remoting work, and what is the "double hop" problem?

Remoting sends commands to another machine over a secure channel: WinRM (WS-Management) on Windows, or SSH, which PowerShell 7 supports on all platforms. Enter-PSSession opens an interactive session, Invoke-Command -ComputerName a, b -ScriptBlock { ... } runs a command on many machines at once. Results come back as deserialized objects: you keep the property values but lose the methods. The double hop appears when the remote machine must use your credentials to reach a third machine: by default credentials are not forwarded, so it fails unless you configure delegation or use a different design. Jobs (Start-Job, thread jobs, ForEach-Object -Parallel in PowerShell 7) provide local parallelism. See Remoting, Jobs and Parallelism.

26. How do you handle secrets and keep PowerShell safe in production?

Never hard-code passwords or tokens, and do not commit them. SecureString only protects a value in memory and is not a general secret store (outside Windows it offers little real protection). Use the SecretManagement module with a vault (an OS store, Azure Key Vault and similar), or your platform's identity features. Run with least privilege (a dedicated account, Just Enough Administration for delegated tasks), turn on script block logging and transcription so activity can be audited, and consider Constrained Language Mode on locked-down machines. Avoid Invoke-Expression with any input you did not write: building commands from strings is how injection happens. See Security and Secrets.

Experiment: how scope behaves

This stand-in models PowerShell's scope chain. Each scope has its own variables; reading walks up the chain through the callers (dynamic scope), and writing always creates a local variable unless you name a scope. This is a model of the rules, not the real engine.

The matching PowerShell, read-only:

POWERSHELL
$x = 1
function Change-X { $x = $x + 1; "inside: $x" }   # reads 1, creates a local 2
Change-X
"outside: $x"                                       # still 1

function Outer { $secret = 'from Outer'; Inner }
function Inner { "Inner sees: $secret" }            # sees its caller's variable
Outer

function Change-Global { $script:x = 99 }           # writes to the script scope
Change-Global
"after: $x"
JS Stand-in: a dynamic scope chain

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice the third block of output: Inner sees from Outer when Outer calls it, and undefined when the top-level code calls it. JavaScript functions normally look up variables where they were written (lexical scope); PowerShell looks where they were called from.

Experiment: the comparison operators

The next stand-in implements the rules from questions 13 and 14: case-insensitive comparison by default, an array on the left turning -eq into a filter, and -contains and -in as the Boolean alternatives.

JS Stand-in: -eq, -ceq, -contains and -in

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The last line is the lesson of question 14: when you want a yes or no answer, use -contains or -in, not -eq with an array on the left.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why should Format-Table come last in a pipeline?

    Format-* cmdlets output objects meant only for display. Later commands such as Export-Csv would receive them instead of your data.
  2. 2. A function assigns $x = 5 where a parent scope already has $x = 1. What is the parent's value afterwards?

    Assignment creates a new local variable in the function's scope. Use $script: or $global: to write to a parent scope explicitly.
  3. 3. What does @(1,2,3,2) -eq 2 return?

    With an array on the left, -eq filters and returns the matching elements. Use -contains or -in for a Boolean.
  4. 4. Which statement about the execution policy is correct?

    Code can reach the engine in other ways, so the execution policy only helps against accidents. Use permissions, application control and logging for security.
  5. 5. An external program fails inside your script, but PowerShell keeps going. Where do you look for the failure?

    Native programs set an exit code instead of throwing. Check $LASTEXITCODE (or $?) after calls that matter.

Summary

  • PowerShell passes objects, so you filter and select on properties; format only at the end of a pipeline.
  • Know the 5.1 versus 7 split: .NET Framework and Windows-only versus modern, cross-platform .NET.
  • Operators are case-insensitive words; an array on the left turns -eq into a filter, so use -contains and -in for yes and no.
  • Functions return everything they output; scope is dynamic and assignment is always local.
  • Non-terminating errors do not trigger catch until you use -ErrorAction Stop; native commands set $LASTEXITCODE.
  • The execution policy is not a security boundary; manage secrets with a vault, run with least privilege and log script blocks.
  • Remoting returns deserialized objects, and the double hop needs a design, not a trick.

Finally, keep the PowerShell cheat sheet open for quick lookups.