Nibble and Bit

Skip Navigation Links
3/4/25

PowerShell 7 Silent Fails (Commands that won't work with Try/Catch)

Set-StrictMode -Version Latest Forces all variables to be declared (no implicit variables) Scope - Script
$ErrorActionPreference = 'Stop' Throw erros to work with Try Catch Scope - Script
-ErrorAction Stop Throw erros to work with Try Catch Scope - Single Line



Command Description Fail Silently Failure Condition
New-Item Creates a file or folder Yes Item Exists
Remove-Item Deletes Files / Folders Yes If file does NOT exist
Copy-Item Copies Files/Folders Yes If destination / target exists
Move-Item Moves files / folders Yes If target exists
Rename-Item Renames files / folders Yes If target exists
Set-Item Changes Property Values Yes If item does NOT exist
Test-Path Checks to see if a file / folder exists NO Retuens $true / $false
Get-ChildItem Dir (DOS) / ls (Unix) Yes No items found
Get-Content Reads a file Yes File does NOT exist
Set-Content Writes to a file Yes If the flie is locked
Add-Content Appends to a file Yes If he file is locked

Revised Feb 20, 2025