1

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'
3
  • try { } catch {}? Aug 2, 2018 at 13:11
  • Something like this? Sorry about formatting... try { Get-ChildItem $source1 -Recurse | Sort-Object -Property CreationTime -Descending | Select-Object -First 1 | Copy-Item -Destination $dest1 -Erroraction 'stop' } catch { Write-Verbose 'copying file' }
    – kissland
    Aug 2, 2018 at 13:13
  • Get-Help about_Try_Catch_Finally
    – LotPings
    Aug 2, 2018 at 14:08

1 Answer 1

2

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
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .