PowerShell script creates a computer list from an AD OU, and add exclusions to a specific file via Invoke-Command My code is working, however, in the foreach section, when it runs the invoke-command if I don't use "-ErrorAction SilentlyContinue" the red error text that states a computer cannot be connected to shows up. The code still works fine, I just wanted to know if there is a way to add "nicer" text, so that when the invoke-command fails to connect, it'll show "This computer cannot be reached"

Any help is appreciated.  

$output_dir = "$env:USERPROFILE\Desktop\ad_computer_list"
$output_file = "$output_dir\computerlist.txt"
$computers_input_file = "computerlist.txt"
$script_name = "Create Spectrum Protect exclusions script"

Import-Module ActiveDirectory
New-Item -Force -ItemType Directory $output_dir | Out-Null
New-Item -Force -ItemType File $output_file

Set-Location $env:USERPROFILE\Desktop\ad_computer_list

#
# MAIN code body
#

Write-Host "`n`n################################################################" -ForegroundColor Gray
Write-Host "Running $($script_name)..." -ForegroundColor Cyan
Write-Host "################################################################" -ForegroundColor Gray

function Get-ComputerList {
param(
$OU_Base = $(Read-Host "Enter distinguished name of OU")
)

Get-ADComputer -Filter "*" -SearchBase "$OU_Base" | 
Select-Object -ExpandProperty Name | 
Sort | 
Out-File -Encoding ASCII -FilePath $env:USERPROFILE\Desktop\ad_computer_list\computerlist.txt

try {
# Get entries in text file, strip away whitespace, blank lines and lines starting with '#' characters
$computers = Get-Content $computers_input_file -ErrorAction Stop | Where-Object { ($_.Trim() -ne '') -and ($_ -NotMatch "^#") }
}
catch {
    Write-Error "Unable to open $computers_input_file. Please create or move the file to the same directory as this script."
break
}

Write-Verbose "$($computers.Count) computer entries found in input file."

foreach ($computer in $computers) {

Write-Host "Opening connection to $computer ..."

try {Invoke-Command -ErrorAction SilentlyContinue -ComputerName $computer -ScriptBlock {if(!(Get-Content -Path "C:\program files\tivoli\tsm\baclient\dsm.opt" -ErrorAction SilentlyContinue | Select-String "Excludedir", "Program Files"))

{Write-Host "Spectrum Protect is not installed or exclusions already exist." -ForegroundColor "Yellow"} 

else 

{add-content -Path "C:\program files\tivoli\tsm\baclient\dsm.opt" -Value `r`n'EXCLUDE.DIR "C:\Program Files"'}}

} catch {
    Write-Host "Cannot connect to computer!"
}

}

}
Get-ComputerList
Your try block will be unable to pass the error to catch block if you don't use -ErrorAction Stop. Also there is an -ErrorAction Ignore on powershell v3.0+ that completely ignores the error.