0
0
PowerShellscripting~10 mins

Terminating vs non-terminating errors in PowerShell - Interactive Practice

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

Complete the code to catch a terminating error using Try-Catch.

PowerShell
try {
    Get-Item 'C:\nonexistentfile.txt' -ErrorAction Stop
} catch [[1]] {
    Write-Host 'Error caught'
}
Drag options to blanks, or click blank then click option'
AException
BError
CWarning
DMessage
Attempts:
3 left
💡 Hint
Common Mistakes
Using catch without specifying Exception may not catch all terminating errors.
Using catch with 'Error' or 'Warning' is incorrect syntax.
2fill in blank
medium

Complete the code to continue execution after a non-terminating error.

PowerShell
Get-Item 'C:\nonexistentfile.txt' -ErrorAction [1]
Drag options to blanks, or click blank then click option'
AStop
BContinue
CIgnore
DSilentlyContinue
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Stop' causes the script to stop on errors.
Using 'Continue' displays the error message but continues execution.
3fill in blank
hard

Fix the error in the Try-Catch block to handle terminating errors properly.

PowerShell
try {
    Get-Item 'C:\nonexistentfile.txt' -ErrorAction [1]
} catch {
    Write-Host 'Caught error'
}
Drag options to blanks, or click blank then click option'
ASilentlyContinue
BStop
CContinue
DIgnore
Attempts:
3 left
💡 Hint
Common Mistakes
Using SilentlyContinue prevents the error from being caught.
Using 'Continue' or 'Ignore' will not make the error terminating.
4fill in blank
hard

Fill both blanks to create a hashtable of files with size greater than 1MB.

PowerShell
$files = Get-ChildItem | Where-Object { $_.Length [1] 1MB } | ForEach-Object { @{ Name = $_.Name; Size = $_.Length [2] 1MB } }
Drag options to blanks, or click blank then click option'
A>
B<
C/
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > filters smaller files.
Using * instead of / causes incorrect size calculation.
5fill in blank
hard

Fill all three blanks to create a dictionary of file names and sizes for files larger than 500KB.

PowerShell
$result = @{
    [1] = Get-ChildItem | Where-Object { $_.Length [2] 500KB } | ForEach-Object { $_.Name }
    [3] = Get-ChildItem | Where-Object { $_.Length [2] 500KB } | ForEach-Object { $_.Length }
}
Drag options to blanks, or click blank then click option'
ANames
BSize
C>
DFiles
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > filters smaller files.
Mixing up dictionary keys causes confusion.