# Catching error in Text file

**URL:** <https://forums.powershell.org/t/catching-error-in-text-file/4378>\
**Category:** PowerShell Help\
**Created:** [June 16, 2015, 5:50pm UTC](https://forums.powershell.org/t/catching-error-in-text-file/4378 "2015-06-16T17:50:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![thomas-cuddy](https://avatars.discourse-cdn.com/v4/letter/t/57b2e6/32.png) [@thomas-cuddy](https://forums.powershell.org/u/thomas-cuddy)\
**Post date:** [June 16, 2015, 5:50pm UTC](https://forums.powershell.org/t/catching-error-in-text-file/4378/1 "2015-06-16T17:50:17Z")

</div>

Morning,

I’ve built my script and it is working correctly for updating and changing the URL as I require. The second part is if an error is thrown out I want the URL, Date/time and the error to be put in to a text file.

At the moment it is recording the URL and Date/time for everything and if an error it is capturing date/time, URL and the Error.

Because the script is running a lot of times I just want it to return a result to the notepad file on occasions where there is an error. What is it I’m doing wrong?

```
function New-SPsiteURL{
[cmdletBinding()]
param(
    #[Parameter]
    [String]$logFile = 'c:\Powershell Exports\Url to title errors.txt'
    )

foreach ($line in (import-csv "C:\Powershell Exports\Newsitelist.csv")) {
        $line.URL
        $line.title    
    
#try{   
        get-spweb -identity $line.url -ea silentlycontinue -ev myerr | set-spweb -relativeurl $line.title   
# }
#Catch{
        
        Get-date | Out-file $logfile -append
        $line.url | Out-File $LogFile -append
        $myerr | Out-File $logfile -append                  
 # }
        }

}
```

---

<div class="post-metadata">

**Author:** ![simon-wahlin](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/simon-wahlin/32/407_2.png) [@simon-wahlin](https://forums.powershell.org/u/simon-wahlin)\
**Post date:** [June 16, 2015, 5:59pm UTC](https://forums.powershell.org/t/catching-error-in-text-file/4378/2 "2015-06-16T17:59:11Z")

</div>

Try{}Catch{} only catches terminating errors.

-ea silentlycontinue will make all errors non-terminating and there by not cought, change this to -ErrorAction Stop to make the errors terminating.

---

<div class="post-metadata">

**Author:** ![rdtechie](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rdtechie/32/273_2.png) [@rdtechie](https://forums.powershell.org/u/rdtechie)\
**Post date:** [June 16, 2015, 6:19pm UTC](https://forums.powershell.org/t/catching-error-in-text-file/4378/3 "2015-06-16T18:19:39Z")

</div>

Also take a look at this nice function. I’m using it in my scripts:

[Function - Write-Log](https://gallery.technet.microsoft.com/scriptcenter/Write-Log-PowerShell-999c32d0)

---

<div class="post-metadata">

**Author:** ![thomas-cuddy](https://avatars.discourse-cdn.com/v4/letter/t/57b2e6/32.png) [@thomas-cuddy](https://forums.powershell.org/u/thomas-cuddy)\
**Post date:** [June 17, 2015, 5:55pm UTC](https://forums.powershell.org/t/catching-error-in-text-file/4378/4 "2015-06-17T17:55:24Z")

</div>

Perfect, Thanks for the info. Great help.

Thomas

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:45pm UTC](https://forums.powershell.org/t/catching-error-in-text-file/4378/5 "2024-05-16T20:45:21Z")

</div>

Closed on May 16, 2024, 8:45pm UTC


