0
0
PowerShellscripting~10 mins

WhatIf and Confirm support in PowerShell - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to enable WhatIf support for the Remove-Item cmdlet.

PowerShell
Remove-Item -Path 'C:\Temp\test.txt' [1]
Drag options to blanks, or click blank then click option'
A-Force
B-Confirm
C-WhatIf
D-Recurse
Attempts:
3 left
💡 Hint
Common Mistakes
Using -Confirm instead of -WhatIf shows a prompt instead of a simulation.
Using -Force or -Recurse does not simulate the action.
2fill in blank
medium

Complete the code to prompt for confirmation before deleting a file.

PowerShell
Remove-Item -Path 'C:\Temp\data.log' [1]
Drag options to blanks, or click blank then click option'
A-Force
B-WhatIf
C-Verbose
D-Confirm
Attempts:
3 left
💡 Hint
Common Mistakes
Using -WhatIf only simulates the action without prompting.
Using -Force bypasses confirmation prompts.
3fill in blank
hard

Fix the error in the function to support WhatIf and Confirm parameters correctly.

PowerShell
function Remove-MyFile {
    param(
        [string]$Path
    )
    Remove-Item -Path $Path [1]
}
Drag options to blanks, or click blank then click option'
A-WhatIf:$true -Confirm:$true
B-Confirm
C-WhatIf
D-Force
Attempts:
3 left
💡 Hint
Common Mistakes
Using only -WhatIf or -Confirm without values inside a function.
Using -Force disables confirmation and simulation.
4fill in blank
hard

Fill both blanks to add WhatIf and Confirm support to this advanced function.

PowerShell
function Remove-MyFolder {
    param(
        [string]$FolderPath,
        [switch]$WhatIf,
        [switch]$Confirm
    )
    Remove-Item -Path $FolderPath [1] [2]
}
Drag options to blanks, or click blank then click option'
A-WhatIf:$WhatIf
B-Confirm:$Confirm
C-Force
D-Recurse
Attempts:
3 left
💡 Hint
Common Mistakes
Hardcoding -WhatIf or -Confirm without using the parameters.
Forgetting to pass both parameters.
5fill in blank
hard

Fill all three blanks to create a function that supports WhatIf and Confirm and uses them in Remove-Item.

PowerShell
function Remove-ItemSafe {
    param(
        [string]$TargetPath,
        [switch]$WhatIf,
        [switch]$Confirm
    )
    Remove-Item [1] [2] [3]
}
Drag options to blanks, or click blank then click option'
A-Path $TargetPath
B-Confirm:$Confirm
C-WhatIf:$WhatIf
D-Force
Attempts:
3 left
💡 Hint
Common Mistakes
Not passing the path parameter correctly.
Mixing up the order of parameters.
Not using the switch values with :$ syntax.