Home »
MCQs
PowerShell MCQs (Multiple-Choice Questions)
Practice these PowerShell MCQs (Multiple-Choice Questions) with answers and explanations to test your knowledge of PowerShell commands, scripting, variables, pipelines, functions, modules, remoting, error handling, objects, and automation.
PowerShell MCQs
These PowerShell multiple-choice questions cover fundamental and intermediate concepts, including cmdlets, variables, operators, objects, pipelines, filtering, formatting, functions, scripts, modules, remoting, jobs, and error handling.
List of PowerShell MCQs
The following PowerShell MCQs are useful for students, developers, system administrators, automation engineers, and candidates preparing for technical interviews and certification examinations.
1. What is PowerShell primarily used for?
- Image editing
- Command-line administration and automation
- Database-only development
- Web browser development
Answer: B) Command-line administration and automation
Explanation:
PowerShell is a command-line shell and scripting language designed for automation, administration, and management tasks.
2. Which file extension is commonly used for a PowerShell script?
.ps1
.bat
.cmd
.sh
Answer: A) .ps1
Explanation:
PowerShell script files normally use the .ps1 extension.
3. What is a PowerShell cmdlet?
- A compiled Windows driver
- A lightweight PowerShell command designed for a specific task
- A database table
- An HTML element
Answer: B) A lightweight PowerShell command designed for a specific task
Explanation:
Cmdlets are specialized PowerShell commands that perform specific operations and generally follow a Verb-Noun naming convention.
4. Which cmdlet displays information about PowerShell commands and concepts?
Get-Command
Get-Help
Get-Info
Show-Help
Answer: B) Get-Help
Explanation:
Get-Help displays information about PowerShell commands, concepts, providers, scripts, and other help topics.
5. Which cmdlet is used to discover available PowerShell commands?
Get-Command
Get-Available
Find-CommandOnly
Search-Command
Answer: A) Get-Command
Explanation:
Get-Command retrieves commands available on the system, including cmdlets, functions, aliases, scripts, and applications.
6. Which symbol represents the PowerShell pipeline operator?
>
|
->
::
Answer: B) |
Explanation:
The pipe character | sends the output objects from one command to another command.
7. What does PowerShell primarily pass through a pipeline?
- Only plain text
- Only file names
- Objects
- Only XML documents
Answer: C) Objects
Explanation:
PowerShell pipelines pass .NET objects between commands, allowing subsequent commands to work with their properties and methods.
8. Which syntax is used to create a PowerShell variable named name?
var name = "John"
$name = "John"
@name = "John"
#name = "John"
Answer: B) $name = "John"
Explanation:
PowerShell variables are identified by a dollar sign. The assignment $name = "John" stores the string in the variable.
9. Which variable contains the most recent error records?
$Error
$Errors
$LastError
$Exception
Answer: A) $Error
Explanation:
$Error stores recent PowerShell error records, with the most recent error at index 0.
10. Which automatic variable indicates whether the last operation succeeded?
$Success
$?
$Result
$Status
Answer: B) $?
Explanation:
$? contains a Boolean value indicating whether the last operation succeeded. For native commands, the result is also related to the command's exit code.
11. Which cmdlet is commonly used to retrieve running processes?
Get-Process
Get-Task
Read-Process
Find-Process
Answer: A) Get-Process
Explanation:
Get-Process retrieves process objects representing processes running on the computer.
12. Which cmdlet retrieves files and directories from a location?
Get-ChildItem
Get-Directory
Read-Folder
Get-FilesOnly
Answer: A) Get-ChildItem
Explanation:
Get-ChildItem retrieves items such as files and directories from a specified location.
13. Which cmdlet is used to filter objects in a PowerShell pipeline?
Where-Object
Filter-Object
Select-Filter
Find-Object
Answer: A) Where-Object
Explanation:
Where-Object selects objects based on property values or a filtering script block.
14. What does $_ represent inside a typical Where-Object script block?
- The current pipeline object
- The current computer name
- The previous command
- The current PowerShell version
Answer: A) The current pipeline object
Explanation:
Inside a pipeline script block, $_ refers to the current object being processed.
15. Which cmdlet selects specific properties from objects?
Select-Object
Choose-Property
Get-Property
Pick-Object
Answer: A) Select-Object
Explanation:
Select-Object can select specified properties, create calculated properties, or select a specified number of objects.
16. Which cmdlet sorts objects by property values?
Order-Object
Sort-Object
Arrange-Object
Order-By
Answer: B) Sort-Object
Explanation:
Sort-Object sorts objects by one or more specified properties. It can also sort in descending order.
17. Which operator is commonly used for equality comparison in PowerShell?
==
=
-eq
:=
Answer: C) -eq
Explanation:
PowerShell uses comparison operators such as -eq, -ne, -gt, and -lt.
18. Which operator means "not equal to" in PowerShell?
-neq
-ne
!=
-not-equal
Answer: B) -ne
Explanation:
The -ne comparison operator tests whether two values are not equal.
19. Which operator is used for logical AND?
-and
-all
-both
-join
Answer: A) -and
Explanation:
The -and operator returns true when both Boolean expressions evaluate to true.
20. Which operator performs string matching using a regular expression?
-match
-regex
-pattern
-regexp
Answer: A) -match
Explanation:
The -match operator performs regular-expression matching against strings.
21. Which cmdlet executes an operation for each object received from a pipeline?
ForEach-Object
Each-Object
Process-Object
Loop-Object
Answer: A) ForEach-Object
Explanation:
ForEach-Object performs an operation against each input object. The current object can commonly be accessed through $_.
22. Which statement correctly creates an array containing three values?
$a = 1, 2, 3
$a = {1, 2, 3}
$a = <1,2,3>
$a = array(1,2,3)
Answer: A) $a = 1, 2, 3
Explanation:
Comma-separated expressions create an array in PowerShell. Therefore, $a = 1, 2, 3 creates an array containing three elements.
23. Which syntax creates a PowerShell hashtable?
@{Name="John"; Age=30}
#{Name="John"; Age=30}
[Hash](Name="John")
Hashtable(Name="John")
Answer: A) @{Name="John"; Age=30}
Explanation:
PowerShell hashtables use the @{ } syntax. Keys and values are separated using the assignment operator.
24. Which type accelerator explicitly represents a PowerShell hashtable?
[System.Array]
[System.Collections.Hashtable]
[System.Dictionary]
[System.Hash]
Answer: B) [System.Collections.Hashtable]
Explanation:
A standard PowerShell hashtable is an instance of System.Collections.Hashtable.
25. What is the purpose of the [ordered] type accelerator?
- To create an ordered dictionary
- To sort every pipeline automatically
- To create a sorted array only
- To prevent duplicate values
Answer: A) To create an ordered dictionary
Explanation:
[ordered] creates an ordered dictionary whose keys retain the order in which they were added.
26. Which keyword is used to define a PowerShell function?
function
def
func
method
Answer: A) function
Explanation:
PowerShell functions are declared using the function keyword followed by the function name and its script block.
27. Which syntax correctly defines a simple PowerShell function?
function Test-Message { "Hello" }
def Test-Message(): "Hello"
function = Test-Message()
func Test-Message => "Hello"
Answer: A) function Test-Message { "Hello" }
Explanation:
PowerShell functions use a script block enclosed in braces after the function name.
28. Which attribute is commonly used to make a PowerShell function an advanced function?
[AdvancedFunction()]
[CmdletBinding()]
[PowerShellFunction()]
[FunctionBinding()]
Answer: B) [CmdletBinding()]
Explanation:
[CmdletBinding()] enables cmdlet-like behavior and common parameters for advanced functions.
29. Which keyword is commonly used to return a value explicitly from a PowerShell function?
return
yield
output
result
Answer: A) return
Explanation:
The return keyword can explicitly return control and an optional value from a function.
30. Which cmdlet is used to import a PowerShell module?
Import-Module
Load-Module
Use-Module
Open-Module
Answer: A) Import-Module
Explanation:
Import-Module loads a module into the current PowerShell session so its exported commands and other members can be used.
31. Which cmdlet displays modules currently loaded in the PowerShell session?
Get-Module
Show-Module
List-Module
Read-Module
Answer: A) Get-Module
Explanation:
Get-Module displays information about modules loaded in the current session. The -ListAvailable parameter can be used to find installed modules that are available for import.
32. Which cmdlet can be used to display detailed information about a PowerShell object?
Get-Member
Get-ObjectInfo
Show-Type
Inspect-Object
Answer: A) Get-Member
Explanation:
Get-Member displays members such as properties and methods of objects passed to it.
33. Which cmdlet is used to display output in a customized table-like format?
Format-Table
Format-Grid
Show-Table
Convert-Table
Answer: A) Format-Table
Explanation:
Format-Table formats objects as a table for display in the PowerShell host.
34. Which cmdlet formats objects as a list of properties?
Format-List
Format-Properties
Show-List
Display-List
Answer: A) Format-List
Explanation:
Format-List displays object properties in a list-oriented format.
35. Which cmdlet measures properties such as the number of objects in a pipeline?
Measure-Object
Count-Object
Measure-Items
Calculate-Object
Answer: A) Measure-Object
Explanation:
Measure-Object can count objects and calculate numerical statistics such as sum, average, minimum, and maximum for appropriate properties.
36. Which cmdlet writes text to the PowerShell host display?
Write-Host
Print-Console
Display-Text
Console-Write
Answer: A) Write-Host
Explanation:
Write-Host writes output directly to the PowerShell host display. It is intended for host-oriented display rather than general pipeline output.
37. Which cmdlet reads input from the user at the console?
Read-Host
Get-ConsoleInput
Read-Console
Input-Host
Answer: A) Read-Host
Explanation:
Read-Host reads a line of input from the user in the console.
38. Which common parameter causes a command to ask for confirmation before performing an operation?
-Confirm
-Ask
-Prompt
-Approve
Answer: A) -Confirm
Explanation:
The -Confirm common parameter requests confirmation before a command performs an operation that supports confirmation.
39. Which common parameter shows what a command would do without actually performing the operation?
-WhatIf
-Preview
-Simulate
-TestRun
Answer: A) -WhatIf
Explanation:
The -WhatIf parameter allows supported commands to describe the operation they would perform without executing it.
40. Which structure is used for exception handling in PowerShell?
try/catch/finally
attempt/handle/end
error/catch/end
try/error/stop
Answer: A) try/catch/finally
Explanation:
PowerShell uses try, catch, and optionally finally to handle terminating errors and perform cleanup.
41. Which option can make a non-terminating error become terminating for a command?
-ErrorAction Stop
-Error Stop
-StopOnError
-TerminateError
Answer: A) -ErrorAction Stop
Explanation:
-ErrorAction Stop escalates applicable non-terminating errors into terminating errors so that they can be handled by catch.
42. Which preference variable controls the default behavior for many non-terminating errors?
$ErrorActionPreference
$ErrorPreference
$ActionPreference
$ExceptionPreference
Answer: A) $ErrorActionPreference
Explanation:
$ErrorActionPreference controls how PowerShell responds to errors in the current scope and child scopes. Values include Continue, Stop, SilentlyContinue, and others.
43. Which keyword explicitly generates a terminating error in PowerShell?
throw
raise
error
fail
Answer: A) throw
Explanation:
The throw keyword generates a script-terminating error unless the error is handled by an appropriate error-handling construct.
44. Which cmdlet can be used to run a command on a remote computer?
Invoke-Command
Remote-Command
Run-Remote
Start-RemoteCommand
Answer: A) Invoke-Command
Explanation:
Invoke-Command can execute commands or script blocks on one or more remote computers.
45. Which cmdlet creates a persistent PowerShell session?
New-PSSession
New-RemoteConnection
Open-PSSession
Connect-PowerShell
Answer: A) New-PSSession
Explanation:
New-PSSession creates a persistent PowerShell session that can be reused for multiple commands. Such sessions can be used with Invoke-Command.
46. Which cmdlet starts an interactive session with a remote computer?
Enter-PSSession
Start-RemoteSession
Open-PSSession
Connect-RemoteHost
Answer: A) Enter-PSSession
Explanation:
Enter-PSSession starts an interactive PowerShell session with a remote computer or an existing PSSession.
47. Which automatic variable contains the exit code of the last native program that ran?
$LASTEXITCODE
$EXITCODE
$LASTSTATUS
$NativeExit
Answer: A) $LASTEXITCODE
Explanation:
$LASTEXITCODE contains the exit code returned by the last native program that ran. A zero exit code conventionally indicates success.
48. Which syntax correctly creates a simple conditional statement in PowerShell?
if ($x -gt 10) { "Large" }
if $x > 10 then "Large"
if ($x > 10): "Large"
when ($x -gt 10) { "Large" }
Answer: A) if ($x -gt 10) { "Large" }
Explanation:
PowerShell uses the if keyword followed by a Boolean expression and a script block containing the statements to execute.
49. What does the following PowerShell command output?
$numbers = 1, 2, 3
$numbers | Where-Object { $_ -gt 1 }
- Only 1
- 1 and 2
- 2 and 3
- 1, 2, and 3
Answer: C) 2 and 3
Explanation:
Where-Object keeps only values for which the condition is true. The expression $_ -gt 1 is true for 2 and 3, so those two values are returned.
50. What does the following PowerShell command return?
Get-Process |
Where-Object { $_.CPU -gt 100 } |
Select-Object Name, CPU |
Sort-Object CPU -Descending
- All processes sorted by process name
- Processes with CPU greater than 100, showing Name and CPU, sorted by CPU descending
- Only the process with the highest CPU usage
- All processes with only their CPU property displayed
Answer: B) Processes with CPU greater than 100, showing Name and CPU, sorted by CPU descending
Explanation:
Get-Process produces process objects. Where-Object filters them to processes whose CPU property is greater than 100. Select-Object keeps only the Name and CPU properties, and Sort-Object CPU -Descending orders the resulting objects from highest CPU value to lowest.
Advertisement
Advertisement