Windows – PowerShell copy-item error catching

powershellwindows

how do I go about catching errors if this fails and output them to a .txt log file?

$ErrorActionPreference = 'stop'

#source1 file location
$source1 = "C:\users\me\desktop\test1"

#dest1 file location
$dest1 = "C:\users\me\desktop\final\"

#finds files in $source1 that are the very latest and copies to $dest1

Get-ChildItem $source1 -Recurse | Sort-Object -Property CreationTime -Descending | Select-Object -First 1 | Copy-Item -Destination $dest1 -ErrorAction 'Stop'

Best Answer

You would want to use a try catch block to capture the error.
With Try/Catch it is important to know that Powershell only catches terminating errors, so you are right to put the -ErrorAction to 'Stop'.

You don't need to use $ErrorActionPreference = 'stop' when you already told the command itself to stop on an error.

This will terminate the command with a fatal error if something goes wrong and then trigger your catch statement.

In your catch you can write whatever code you want to if catch is triggered, in the sample below, if an error is triggered we take the current error object, selects the exception message and outputs it to a file.

The Exception object contains a few other properties you could use instead depending on what information you want.

#source1 file location
$source1 = "C:\users\me\desktop\test1"

#dest1 file location
$dest1 = "C:\users\me\desktop\final\"

#finds files in $source1 that are the very latest and copies to $dest1
try {
Get-ChildItem $source1 -Recurse | Sort-Object -Property CreationTime -Descending | Select-Object -First 1 | Copy-Item -Destination $dest1 -ErrorAction 'Stop'
}
catch{
    $_.Exception.Message | Out-File -FilePath .\errorlog.txt -Encoding utf8
}
Related Topic