PowerShell: Output A List of Recently Created Or Modified Files

Published on
-
2 min read

At times there is need to get a list of files that have been updated. This could for the following reasons:

  • Audit compliance to maintain records of application changes.
  • Backup verification to confirm the right files were backed up.
  • Verification of changed files to confirm which files were added, modified, or deleted during an update.
  • Security checks to ensure that there have been no unauthorised or suspicious files changed or installed through hacking.
  • Troubleshooting Issues after a new application release by seeing a list of changed files can help identify the source of issues.

Based on the information I found online, I put together a PowerShell script that was flexible enough to meet the needs of the above scenarios, as I encountered one of them this week. I'll let you guess the scenario I faced.

At its core, the following PowerShell script uses the Get-ChildItem command to list out all files recursively across all sub-folders, ordered by the created date descending with the addition of handful of optional parameters.

Get-ChildItem -Path C:\My-Path -Recurse -Include *.png | 
			Select -Last 5 CreationTime,LastWriteTime,FullName | 
			Sort-Object -Property CreationTime -Descending | 
			Export-Csv "file-list.csv"

Breakdown of the parameters used:

Parameter/Object Detail Is Optional
-Path The folder path to where files need to be listed. No
-Recurse Get files from the path and its subdirectories Yes
-Include Filter the file output through a path element or pattern,. This only works when the "Recurse" parameter is present. Yes
Select Set the maximum output (-Last) and list of fields to be listed. Yes
Sort-Object Specify field and sort order. Yes
Export-Csv Export the list of files list to a CSV. Yes

If the files need to be sorted by last modified date, the Sort-Object property needs to be set to "LastWriteTime".

When the script is run, you'll see the results rendered in the following way:

CreationTime        LastWriteTime       FullName
------------        -------------       --------
25/05/2023 20:33:44 25/05/2023 20:33:44 X:\Downloads\synology\Screenshot 2023-05-25 at 20.33.38.png
16/05/2023 14:18:21 16/05/2023 14:18:21 X:\Downloads\synology\Screenshot 2023-05-16 at 14.18.15.png

Further Information

Before you go...

If you've found this post helpful, you can buy me a coffee. It's certainly not necessary but much appreciated!

Buy Me A Coffee

Leave A Comment

If you have any questions or suggestions, feel free to leave a comment. I do get inundated with messages regarding my posts via LinkedIn and leaving a comment below is a better place to have an open discussion. Your comment will not only help others, but also myself.