PowerShell/docs/list-unused-files.md

99 lines
2.7 KiB
Markdown
Raw Normal View History

2024-11-08 12:38:20 +01:00
The *list-unused-files.ps1* Script
===========================
2021-11-08 21:36:42 +01:00
2023-12-07 20:24:45 +01:00
This PowerShell script scans a directory tree and lists unused files (no read/write access since a number of days).
2021-11-08 21:36:42 +01:00
2023-07-29 10:04:38 +02:00
Parameters
----------
2021-11-08 21:36:42 +01:00
```powershell
2024-11-08 12:35:11 +01:00
/home/markus/Repos/PowerShell/scripts/list-unused-files.ps1 [[-path] <String>] [[-days] <Int32>] [<CommonParameters>]
2021-11-08 21:36:42 +01:00
2023-12-07 20:24:45 +01:00
-path <String>
Specifies the path to the directory tree (current working dir by default)
2021-11-08 21:36:42 +01:00
Required? false
Position? 1
2023-12-07 20:24:45 +01:00
Default value "$PWD"
2021-11-08 21:36:42 +01:00
Accept pipeline input? false
Accept wildcard characters? false
2023-12-07 20:24:45 +01:00
-days <Int32>
Specifies the number of days (100 by default)
2021-11-08 21:36:42 +01:00
Required? false
Position? 2
Default value 100
Accept pipeline input? false
Accept wildcard characters? false
[<CommonParameters>]
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
```
2023-07-29 10:04:38 +02:00
Example
-------
2021-11-08 21:36:42 +01:00
```powershell
2023-12-07 20:24:45 +01:00
PS> ./list-unused-files.ps1 C:\Windows
...
2024-11-08 12:35:11 +01:00
✅ Found 43729 unused files (no access for 100 days) within 📂C:\Windows in 113 sec
2021-11-08 21:36:42 +01:00
```
2023-07-29 10:04:38 +02:00
Notes
-----
2022-11-17 19:46:02 +01:00
Author: Markus Fleschutz | License: CC0
2021-11-08 21:36:42 +01:00
2023-07-29 10:04:38 +02:00
Related Links
-------------
2021-11-08 21:36:42 +01:00
https://github.com/fleschutz/PowerShell
2023-07-29 10:04:38 +02:00
Script Content
--------------
2022-11-17 20:05:34 +01:00
```powershell
2022-11-17 20:02:26 +01:00
<#
.SYNOPSIS
Lists unused files in a directory tree
.DESCRIPTION
2023-12-07 20:24:45 +01:00
This PowerShell script scans a directory tree and lists unused files (no read/write access since a number of days).
.PARAMETER path
Specifies the path to the directory tree (current working dir by default)
.PARAMETER days
Specifies the number of days (100 by default)
2022-11-17 20:02:26 +01:00
.EXAMPLE
2023-12-07 20:24:45 +01:00
PS> ./list-unused-files.ps1 C:\Windows
...
2024-11-08 12:35:11 +01:00
✅ Found 43729 unused files (no access for 100 days) within 📂C:\Windows in 113 sec
2022-11-17 20:02:26 +01:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
2023-12-07 20:24:45 +01:00
param([string]$path = "$PWD", [int]$days = 100)
2022-11-17 20:02:26 +01:00
try {
2023-12-07 20:24:45 +01:00
$stopWatch = [system.diagnostics.stopwatch]::startNew()
2022-11-17 20:02:26 +01:00
2023-12-07 20:24:45 +01:00
$path = Resolve-Path "$path"
Write-Progress "Scanning $path for unused files..."
2022-11-17 20:02:26 +01:00
2023-12-07 20:24:45 +01:00
$cutOffDate = (Get-Date).AddDays(-$Days)
[int]$count = 0
Get-ChildItem -path $path -recurse | Where-Object {$_.LastAccessTime -le $cutOffDate} | Foreach-Object {
"📄$($_.FullName)"
$count++
}
Write-Progress -completed " "
[int]$elapsed = $stopWatch.Elapsed.TotalSeconds
2024-11-08 12:35:11 +01:00
"✅ Found $count unused files (no access for $days days) within 📂$path in $elapsed sec"
2022-11-17 20:02:26 +01:00
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}
2022-11-17 20:05:34 +01:00
```
2022-11-17 20:02:26 +01:00
2024-11-20 11:52:20 +01:00
*(generated by convert-ps2md.ps1 as of 11/20/2024 11:51:57)*