# Want to create a log file of all output

**URL:** <https://forums.powershell.org/t/want-to-create-a-log-file-of-all-output/9233>\
**Category:** PowerShell Help\
**Created:** [August 24, 2017, 4:40pm UTC](https://forums.powershell.org/t/want-to-create-a-log-file-of-all-output/9233 "2017-08-24T16:40:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeff-taylor](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/jeff-taylor/32/1015_2.png) [@jeff-taylor](https://forums.powershell.org/u/jeff-taylor)\
**Post date:** [August 24, 2017, 4:40pm UTC](https://forums.powershell.org/t/want-to-create-a-log-file-of-all-output/9233/1 "2017-08-24T16:40:24Z")

</div>

Hello,

I have this code I want to run in Azure:

```
$users = import-csv c:\Users.csv
foreach ($user in $Users) {Get-Mailbox $user.samAccountName -Credential $creds }
```
 and I expect errors but how do I also log them to a text file, please? If it can simply return “mailbox does not exist” and “mailbox exists”, instead of many lines of error per object queried, all the better.

thanks

---

<div class="post-metadata">

**Author:** ![miracles1315](https://avatars.discourse-cdn.com/v4/letter/m/53a042/32.png) [@miracles1315](https://forums.powershell.org/u/miracles1315)\
**Post date:** [August 24, 2017, 8:30pm UTC](https://forums.powershell.org/t/want-to-create-a-log-file-of-all-output/9233/2 "2017-08-24T20:30:39Z")

</div>

I wouldn’t suggest just returning something like “mailbox doesn’t exist” because when a mailbox can’t be found, it gives an error that says the object couldn’t be found on a specific GC. Most likely it’s because the mailbox object doesn’t exist, but it also could be due to replication issues in the AD environment, or some issue with that specific GC. Instead, if you don’t want the entire error dumped into a file, you can grab part of it. For example you could do something like the following. Note that the screen will show the list of mailbox objects it did find. Also, I put code in that also logged a “mailbox found” type message to the same log file as the errors. Hope this helps.

```
$Date = Get-Date -Format yyyyMMdd
$Log = "C:\data\GetMbxResults_$Date.txt"
$Error.Clear()
$Users = Import-Csv -Path C:\data\samaccts.txt
ForEach($User in $Users)
{
  Try
  {
    Get-Mailbox $User.SamAccountName -ErrorAction Stop -Credentials $creds
    #You can have the following two lines if you want to output the successes to the log file.
    $SuccessMsg = "$(Get-Date): [INFORMATION]: Mailbox for $($User.SamAccountName) exists."
    $SuccessMsg | Out-File -FilePath $Log -Append
  }
  Catch
  {
    $ErrorMsg = "$(Get-Date): [ERROR]: $($_.Exception.Message)"
    $ErrorMsg | Out-File -FilePath $Log -Append
  }
}
```

---

<div class="post-metadata">

**Author:** ![miracles1315](https://avatars.discourse-cdn.com/v4/letter/m/53a042/32.png) [@miracles1315](https://forums.powershell.org/u/miracles1315)\
**Post date:** [August 24, 2017, 8:34pm UTC](https://forums.powershell.org/t/want-to-create-a-log-file-of-all-output/9233/3 "2017-08-24T20:34:56Z")

</div>

Added the -Credentials $creds into the example to match the original code you listed.

---

<div class="post-metadata">

**Author:** ![jeff-taylor](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/jeff-taylor/32/1015_2.png) [@jeff-taylor](https://forums.powershell.org/u/jeff-taylor)\
**Post date:** [August 25, 2017, 5:47pm UTC](https://forums.powershell.org/t/want-to-create-a-log-file-of-all-output/9233/4 "2017-08-25T17:47:10Z")

</div>

Thanks Kevyn, I will test this next week

---

<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:37pm UTC](https://forums.powershell.org/t/want-to-create-a-log-file-of-all-output/9233/5 "2024-05-16T20:37:43Z")

</div>


