# Writing Powershell script output to EventData in Event log

**URL:** <https://forums.powershell.org/t/writing-powershell-script-output-to-eventdata-in-event-log/15423>\
**Category:** PowerShell Help\
**Created:** [December 22, 2020, 8:51am UTC](https://forums.powershell.org/t/writing-powershell-script-output-to-eventdata-in-event-log/15423 "2020-12-22T08:51:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex145](https://avatars.discourse-cdn.com/v4/letter/a/d9b06d/32.png) [@alex145](https://forums.powershell.org/u/alex145)\
**Post date:** [December 22, 2020, 8:51am UTC](https://forums.powershell.org/t/writing-powershell-script-output-to-eventdata-in-event-log/15423/1 "2020-12-22T08:51:25Z")

</div>

Hi all. I’m fairly new to Powershell scripting, and became stumped on this task based on experience & research. Any help would be greatly appreciated! Please see below.

Objective: Output the results of Name, OperatingSystem, OperatingSystemVersion, lastlogondate to an event log. Currently and reasonably, it’s only outputting the message “This is a test” below in the event log. Any suggestions of how to output the results of the script itself to the event log it generates?

`

```
cd c:\scripts

Get-ADComputer -Filter * -Property * | Select-Object -Property Name,OperatingSystem,OperatingSystemVersion,lastlogondate

Write-EventLog -LogName Application -EventID 3000 -EntryType Information -Source ‘AD PC OS Information’ -Message ‘This is a test’
```

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [December 22, 2020, 11:51am UTC](https://forums.powershell.org/t/writing-powershell-script-output-to-eventdata-in-event-log/15423/2 "2020-12-22T11:51:57Z")

</div>

```
cd c:\scripts

$props = 'Name','OperatingSystem','OperatingSystemVersion','LastLogonDate'

$Info = Get-ADComputer -Filter * -Property $props | Select-Object -Property $props

$template = @"
{0} : {1}

{2} : {3}

{4} : {5}

{6} : {7}
"@

$info | ForEach-Object {
    $str = $template -f $($_.psobject.Properties | ForEach-Object {$_.name,$_.value})
    Write-EventLog -LogName Application -EventID 3000 -EntryType Information -Source ‘AD PC OS Information’ -Message $str
}
```

---

<div class="post-metadata">

**Author:** ![alex145](https://avatars.discourse-cdn.com/v4/letter/a/d9b06d/32.png) [@alex145](https://forums.powershell.org/u/alex145)\
**Post date:** [December 23, 2020, 2:24am UTC](https://forums.powershell.org/t/writing-powershell-script-output-to-eventdata-in-event-log/15423/3 "2020-12-23T02:24:24Z")

</div>

This is perfect, thanks Doug!

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [December 23, 2020, 4:08am UTC](https://forums.powershell.org/t/writing-powershell-script-output-to-eventdata-in-event-log/15423/4 "2020-12-23T04:08:25Z")

</div>

@Doug Nice script!

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [December 23, 2020, 6:01am UTC](https://forums.powershell.org/t/writing-powershell-script-output-to-eventdata-in-event-log/15423/5 "2020-12-23T06:01:27Z")

</div>

Lol thanks @Rob it was the only way I could make it look nice with varying value lengths

---

<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:28pm UTC](https://forums.powershell.org/t/writing-powershell-script-output-to-eventdata-in-event-log/15423/6 "2024-05-16T20:28:04Z")

</div>


