Challenge - 5 Problems
WhatIf and Confirm Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the output when using -WhatIf with Remove-Item?
Consider the following PowerShell command:
What will this command output?
Remove-Item -Path 'C:\Temp\file.txt' -WhatIf
What will this command output?
PowerShell
Remove-Item -Path 'C:\Temp\file.txt' -WhatIfAttempts:
2 left
💡 Hint
The -WhatIf parameter shows what would happen without making changes.
✗ Incorrect
The -WhatIf parameter simulates the command and outputs a message describing the action without performing it.
💻 Command Output
intermediate2:00remaining
What happens when Confirm is set to true on Stop-Process?
Given the command:
What will PowerShell do when this runs?
Stop-Process -Name notepad -Confirm:$true
What will PowerShell do when this runs?
PowerShell
Stop-Process -Name notepad -Confirm:$true
Attempts:
2 left
💡 Hint
The -Confirm parameter controls user confirmation prompts.
✗ Incorrect
Setting -Confirm:$true forces PowerShell to ask for confirmation before stopping each process.
📝 Syntax
advanced2:30remaining
Identify the correct way to add WhatIf support in a custom function
You want to add WhatIf support to this PowerShell function:
Which option correctly adds WhatIf support?
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
}Attempts:
2 left
💡 Hint
WhatIf support requires CmdletBinding and ShouldProcess method.
✗ Incorrect
To support -WhatIf, the function must declare CmdletBinding with SupportsShouldProcess and call ShouldProcess before action.
🔧 Debug
advanced2:30remaining
Why does this function not prompt for confirmation despite -Confirm:$true?
Given this function:
When calling Remove-MyFile -Path 'C:\temp\file.txt' -Confirm:$true, no confirmation prompt appears. Why?
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
}Attempts:
2 left
💡 Hint
Confirm support requires CmdletBinding with SupportsShouldProcess in functions.
✗ Incorrect
Without CmdletBinding(SupportsShouldProcess=$true), the function does not support -Confirm and the parameter is ignored.
🚀 Application
expert3: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?
Attempts:
2 left
💡 Hint
Use CmdletBinding with SupportsShouldProcess and call ShouldProcess method.
✗ Incorrect
To support -WhatIf and -Confirm, the function must declare CmdletBinding with SupportsShouldProcess and call ShouldProcess before performing the action.