0
0
PowerShellscripting~20 mins

WhatIf and Confirm support in PowerShell - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
WhatIf and Confirm Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output when using -WhatIf with Remove-Item?
Consider the following PowerShell command:
Remove-Item -Path 'C:\Temp\file.txt' -WhatIf

What will this command output?
PowerShell
Remove-Item -Path 'C:\Temp\file.txt' -WhatIf
APrompt asking 'Are you sure you want to delete C:\Temp\file.txt?'
BWhat if: Performing the operation "Remove File" on target "C:\Temp\file.txt".
CError: Remove-Item does not support -WhatIf parameter.
DFile 'C:\Temp\file.txt' is deleted silently with no output.
Attempts:
2 left
💡 Hint
The -WhatIf parameter shows what would happen without making changes.
💻 Command Output
intermediate
2:00remaining
What happens when Confirm is set to true on Stop-Process?
Given the command:
Stop-Process -Name notepad -Confirm:$true

What will PowerShell do when this runs?
PowerShell
Stop-Process -Name notepad -Confirm:$true
AIt throws an error because -Confirm does not accept boolean values.
BIt stops all notepad processes immediately without asking.
CIt prompts the user to confirm stopping each notepad process.
DIt silently ignores the command without stopping any process.
Attempts:
2 left
💡 Hint
The -Confirm parameter controls user confirmation prompts.
📝 Syntax
advanced
2:30remaining
Identify the correct way to add WhatIf support in a custom function
You want to add WhatIf support to this PowerShell function:
function Remove-MyFile {
  param([string]$Path)
  Remove-Item -Path $Path
}

Which option correctly adds WhatIf support?
PowerShell
function Remove-MyFile {
  param([string]$Path)
  Remove-Item -Path $Path
}
AAdd [CmdletBinding(SupportsShouldProcess=$true)] above function and use if ($PSCmdlet.ShouldProcess($Path)) { Remove-Item -Path $Path }
BAdd param([switch]$WhatIf) and inside function use if ($WhatIf) { Write-Output "WhatIf: $Path" } else { Remove-Item -Path $Path }
CAdd -WhatIf parameter to Remove-Item call directly without changes to function declaration
DAdd SupportsWhatIf=$true in param block and call Remove-Item normally
Attempts:
2 left
💡 Hint
WhatIf support requires CmdletBinding and ShouldProcess method.
🔧 Debug
advanced
2:30remaining
Why does this function not prompt for confirmation despite -Confirm:$true?
Given this function:
function Remove-MyFile {
  param([string]$Path)
  Remove-Item -Path $Path -Confirm:$true
}

When calling Remove-MyFile -Path 'C:\temp\file.txt' -Confirm:$true, no confirmation prompt appears. Why?
PowerShell
function Remove-MyFile {
  param([string]$Path)
  Remove-Item -Path $Path -Confirm:$true
}
APowerShell disables confirmation prompts by default.
BRemove-Item does not support -Confirm parameter.
CThe -Confirm parameter must be passed to the function, not inside Remove-Item.
DThe function does not declare CmdletBinding with SupportsShouldProcess, so -Confirm is ignored.
Attempts:
2 left
💡 Hint
Confirm support requires CmdletBinding with SupportsShouldProcess in functions.
🚀 Application
expert
3:00remaining
How to implement both WhatIf and Confirm support in a custom PowerShell function?
You want to create a PowerShell function that deletes a file and supports both -WhatIf and -Confirm parameters natively. Which code snippet correctly implements this?
A
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='Medium')]
param([string]$Path)
if ($PSCmdlet.ShouldProcess($Path)) {
  Remove-Item -Path $Path
}
B
param([string]$Path, [switch]$WhatIf, [switch]$Confirm)
if ($WhatIf) { Write-Output "WhatIf: Remove $Path" } elseif ($Confirm) { Read-Host 'Confirm deletion?'; Remove-Item -Path $Path } else { Remove-Item -Path $Path }
C
[CmdletBinding()]
param([string]$Path)
Remove-Item -Path $Path -WhatIf:$WhatIf -Confirm:$Confirm
D
param([string]$Path)
Remove-Item -Path $Path -WhatIf -Confirm
Attempts:
2 left
💡 Hint
Use CmdletBinding with SupportsShouldProcess and call ShouldProcess method.