# Create Log file

**URL:** <https://forums.powershell.org/t/create-log-file/6695>\
**Category:** PowerShell Help\
**Created:** [June 22, 2016, 3:08pm UTC](https://forums.powershell.org/t/create-log-file/6695 "2016-06-22T15:08:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bob-withrow](https://avatars.discourse-cdn.com/v4/letter/b/e19adc/32.png) [@bob-withrow](https://forums.powershell.org/u/bob-withrow)\
**Post date:** [June 22, 2016, 3:08pm UTC](https://forums.powershell.org/t/create-log-file/6695/1 "2016-06-22T15:08:14Z")

</div>

I’m new to PowerShell and need to know how would I delete all archived log files (c:\WINDOWS\system32\logfiles) older than 365 days on Windows Server 2008 R2?  
I have figured out, with Don’s help, how to do this using the following script: Get-Childitem \*logfile | where-object { $\_.LastAccessTime -lt ((Get-Date).AddDays(-365)) } | Remove-Item  
But now I need to create a logfile with a date/time stamp to show all the files over 365 days that were deleted?  
Would I export to a CSV file in the same directory?  
Any suggestions, assistance, or advice would be appreciated?  
Thanks,  
Bob

---

<div class="post-metadata">

**Author:** ![csandfeld](https://avatars.discourse-cdn.com/v4/letter/c/df788c/32.png) [@csandfeld](https://forums.powershell.org/u/csandfeld)\
**Post date:** [June 22, 2016, 3:53pm UTC](https://forums.powershell.org/t/create-log-file/6695/2 "2016-06-22T15:53:27Z")

</div>

You might do something like this:

```
Get-Childitem *logfile |
where-object { $_.LastAccessTime -lt ((Get-Date).AddDays(-365)) } | 
ForEach-Object {
    $_ | Remove-Item
    $_ | Select-Object -Property LastWriteTime, LastAccessTime, FullName
} |
Export-Csv -Path "C:\Path\To\DeletedLogs_$(Get-Date -Format 'yyyyMMdd_HHmmss').csv" -NoTypeInformation -UseCulture
```

---

<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:41pm UTC](https://forums.powershell.org/t/create-log-file/6695/3 "2024-05-16T20:41:03Z")

</div>

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


