PowerShell/Scripts/list-unused-files.ps1
2022-09-06 21:42:04 +02:00

32 lines
828 B
PowerShell
Executable File

<#
.SYNOPSIS
Lists unused files in a directory tree
.DESCRIPTION
This PowerShell script scans and lists files in a folder with last access time older than number of days.
.PARAMETER DirTree
Specifies the path to the directory tree
.PARAMETER Days
Specifies the number of days
.EXAMPLE
PS> ./list-unused-files C:\ 100
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([string]$DirTree = "", [int]$Days = 100)
write-host "Listing files in $DirTree with last access time older than $Days days"
try {
$cutOffDate = (Get-Date).AddDays(-$Days)
Get-ChildItem -path $DirTree -recurse | Where-Object {$_.LastAccessTime -le $cutOffDate} | select fullname
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}