Troubleshooting Common Errors
Note
Quick guide to understanding, diagnosing, and fixing the most common PowerShell errors you'll encounter.
Overview
Every PowerShell user encounters errors. The key is knowing how to read error messages, understand what went wrong, and fix the problem. This guide covers the most common errors and their solutions.
How to Read Error Messages
PowerShell error messages contain valuable information:
Get-Item : Cannot find path 'C:\NonExistent\file.txt' because it does not exist.
At line:1 char:1
+ Get-Item "C:\NonExistent\file.txt"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\NonExistent\file.txt:String) [Get-Item], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand
Breaking it down:
- First line: What command failed and why
- At line:X char:Y: Where in your script the error occurred
- CategoryInfo: Type of error (ObjectNotFound, InvalidArgument, etc.)
- FullyQualifiedErrorId: Technical error identifier
Focus on the First Line
The first line usually tells you everything you need to know. The rest provides technical details for deeper debugging.
Common Error Categories
1. File Not Found / Path Not Found
Error Message
What It Means
PowerShell can't find the file or folder you specified.
Common Causes
- Typo in the path
- File doesn't exist
- Using forward slashes instead of backslashes on Windows
- Relative path when you need absolute path
How to Fix
# BAD - Path doesn't exist
Get-Content "C:\NonExistent\file.txt"
# GOOD - Check if path exists first
$path = "C:\Temp\file.txt"
if (Test-Path $path) {
Get-Content $path
} else {
Write-Warning "File not found: $path"
}
# GOOD - Use error handling
try {
Get-Content "C:\Temp\file.txt" -ErrorAction Stop
} catch {
Write-Error "Could not read file: $_"
}
Always Use Test-Path
Before working with files, verify they exist using Test-Path.
2. Access Denied / UnauthorizedAccessException
Error Message
What It Means
You don't have permission to access the file/folder.
Common Causes
- Trying to access system files without admin rights
- File is locked by another process
- Insufficient NTFS permissions
How to Fix
# Solution 1: Run PowerShell as Administrator
# Right-click PowerShell → "Run as Administrator"
# Solution 2: Check permissions
Get-Acl "C:\SecureFolder" | Format-List
# Solution 3: Use -Force (when appropriate)
Remove-Item "C:\file.txt" -Force
# Solution 4: Check if file is in use
$file = "C:\file.txt"
try {
[System.IO.File]::Open($file, 'Open', 'Read', 'None').Dispose()
Write-Output "File is not locked"
} catch {
Write-Warning "File is in use by another process"
}
3. Cannot Bind Parameter / Parameter Not Found
Error Message
What It Means
You used a parameter name that doesn't exist for that cmdlet.
Common Causes
- Typo in parameter name
- Using wrong cmdlet
- Parameter doesn't exist in your PowerShell version
How to Fix
# BAD - Wrong parameter name
Get-Process -ProcessName "powershell" # Should be -Name
# GOOD - Use correct parameter
Get-Process -Name "powershell"
# Check available parameters
Get-Help Get-Process -Parameter *
# Use Tab completion to avoid typos
Get-Process -N<Tab> # Auto-completes to -Name
Use Get-Help to Find Parameters
Get-Help CommandName -Parameter * shows all available parameters.
4. Execution Policy Error
Error Message
What It Means
Your system's execution policy prevents scripts from running.
Common Causes
- Default security settings block scripts
- Group Policy restrictions
How to Fix
# Check current policy
Get-ExecutionPolicy
# Set for current user only (doesn't require admin)
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
# Temporarily bypass for one script
PowerShell.exe -ExecutionPolicy Bypass -File "C:\Scripts\script.ps1"
# Check if Group Policy is blocking
Get-ExecutionPolicy -List
Security Note
Only run scripts you trust! Changing execution policy can expose you to malicious scripts.
5. Cannot Convert Value to Type
Error Message
Cannot convert value "abc" to type "System.Int32". Error: "Input string was not in a correct format."
What It Means
PowerShell can't convert the data to the requested type.
Common Causes
- Trying to convert text to a number
- Invalid date format
- Type mismatch
How to Fix
# BAD - Can't convert text to number
[int]"abc" # ERROR
# GOOD - Validate before converting
$input = "123"
if ($input -match '^\d+$') {
$number = [int]$input
} else {
Write-Warning "Input is not a valid number"
}
# GOOD - Use try/catch for conversion
try {
$number = [int]"abc"
} catch {
Write-Error "Conversion failed: $_"
}
# GOOD - Use -as operator (returns $null on failure)
$number = "abc" -as [int]
if ($null -eq $number) {
Write-Warning "Conversion failed"
}
6. Property Does Not Exist
Error Message
What It Means
You're trying to access a property that doesn't exist on the object.
Common Causes
- Typo in property name
- Object type is different than expected
- Property was removed in newer PowerShell versions
How to Fix
# Find available properties
$process = Get-Process | Select-Object -First 1
$process | Get-Member -MemberType Property
# BAD - Property doesn't exist
$process.InvalidProperty
# GOOD - Use correct property name
$process.Name
# GOOD - Check if property exists
if ($process.PSObject.Properties['CPU']) {
Write-Output $process.CPU
}
7. Object Reference Not Set to an Instance
Error Message
What It Means
You're trying to use an object that is $null.
Common Causes
- Variable was never set
- Command returned no results
- Property doesn't exist
How to Fix
# BAD - Variable is null
$result = $null
$result.ToString() # ERROR
# GOOD - Check for null first
if ($null -ne $result) {
$result.ToString()
} else {
Write-Warning "Result is null"
}
# GOOD - Use null-conditional operator (PowerShell 7+)
$result?.ToString()
# GOOD - Provide default value
$value = $result ?? "Default"
8. The Term Is Not Recognized
Error Message
The term 'Get-FakeCommand' is not recognized as the name of a cmdlet, function, script file, or operable program.
What It Means
PowerShell doesn't know what command you're trying to run.
Common Causes
- Typo in command name
- Module not loaded
- Command doesn't exist
How to Fix
# Check if command exists
Get-Command Get-Process
# Search for partial name
Get-Command *Process*
# Load required module
Import-Module ActiveDirectory
Get-ADUser -Filter *
# Check available modules
Get-Module -ListAvailable
# BAD - Typo
Get-Proces
# GOOD - Correct spelling
Get-Process
9. Index Out of Range
Error Message
What It Means
You're trying to access an array element that doesn't exist.
Common Causes
- Array is smaller than you think
- Using wrong index number
- Array is empty
How to Fix
# BAD - Array only has 3 items (index 0, 1, 2)
$array = @("one", "two", "three")
$array[5] # ERROR
# GOOD - Check array size
if ($array.Count -gt 5) {
$array[5]
} else {
Write-Warning "Array doesn't have 6 items"
}
# GOOD - Use safe indexing
$index = 5
if ($index -lt $array.Count) {
$array[$index]
}
10. Pipeline Input Error
Error Message
What It Means
Pipeline is sending wrong type of object to the command.
Common Causes
- Format cmdlet used too early in pipeline
- Wrong object type
- Misunderstanding how pipeline binding works
How to Fix
# BAD - Format-Table breaks pipeline
Get-Process | Format-Table | Where-Object {$_.CPU -gt 50} # ERROR
# GOOD - Format at the end
Get-Process | Where-Object {$_.CPU -gt 50} | Format-Table
# Check what's in the pipeline
Get-Process | Get-Member # Shows object type
Debugging Techniques
Use -WhatIf to Preview
# See what would happen without actually doing it
Remove-Item "C:\Important\*" -WhatIf
Stop-Service "ImportantService" -WhatIf
Use -Verbose for Details
# See detailed information about what's happening
Copy-Item -Path "C:\source" -Destination "C:\dest" -Verbose
Use Write-Host for Debugging
# Add debug output to see what's happening
foreach ($file in Get-ChildItem) {
Write-Host "Processing: $($file.Name)" -ForegroundColor Yellow
# Your code here
}
Check Variable Values
# See what's in a variable
$myVariable | Format-List *
# See variable type
$myVariable.GetType()
# See if variable is null
if ($null -eq $myVariable) {
Write-Output "Variable is null!"
}
Use Try-Catch for Error Details
try {
# Your code here
Get-Content "C:\file.txt"
} catch {
Write-Error "Error occurred: $($_.Exception.Message)"
Write-Error "Error type: $($_.Exception.GetType().FullName)"
Write-Error "Line number: $($_.InvocationInfo.ScriptLineNumber)"
}
Use $Error Variable
# View recent errors
$Error[0] # Most recent error
$Error[1] # Second most recent
# View full error details
$Error[0] | Format-List * -Force
# Clear error history
$Error.Clear()
Common Mistakes
Not Checking if File Exists
Ignoring Error Messages
Assuming Command Succeeds
# BAD - Doesn't check if copy worked
Copy-Item "C:\source.txt" "D:\backup.txt"
Remove-Item "C:\source.txt" # Deletes even if copy failed!
# GOOD - Verify operation succeeded
try {
Copy-Item "C:\source.txt" "D:\backup.txt" -ErrorAction Stop
if (Test-Path "D:\backup.txt") {
Remove-Item "C:\source.txt"
}
} catch {
Write-Error "Copy failed: $_"
}
Tips & Tricks
Use -ErrorAction to Control Behavior
Save Errors to a Variable
Use -Debug for Step-by-Step Execution
Quick Reference: Error Action Preference
| ErrorAction | What Happens |
|---|---|
Stop | Throws terminating error, stops execution |
Continue | Displays error, continues (default) |
SilentlyContinue | Suppresses error, continues |
Inquire | Asks user how to proceed |
Ignore | Completely ignores error (doesn't log it) |
When to Ask for Help
If you're still stuck:
-
Use Get-Help
-
Search Online
- Copy the error message
- Search: "PowerShell [error message]"
-
Check Microsoft Docs, Stack Overflow, Reddit r/PowerShell
-
Check PowerShell Version
Some errors occur only in specific versions. -
Ask the Community
- Reddit: r/PowerShell
- Stack Overflow: [powershell] tag
- PowerShell Discord/Slack
Related Topics
- Error Handling - Structured error handling
- Logging - Recording and analyzing issues
- Your First Script - Building reliable scripts
- Script Structure - Organizing code to minimize errors