Create Log file

Welcome Forums General PowerShell Q&A Create Log file

This topic contains 1 reply, has 2 voices, and was last updated by

3 years ago.

  • Author
  • #44460

    Topics: 2
    Replies: 2
    Points: 0
    Rank: Member

    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?

  • #44466

    Topics: 4
    Replies: 104
    Points: 80
    Rank: Member

    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

The topic ‘Create Log file’ is closed to new replies.