2024-01-25 13:37:12 +01:00
|
|
|
Script: *remove-old-dirs.ps1*
|
|
|
|
========================
|
2023-05-26 12:20:18 +02:00
|
|
|
|
2023-07-29 09:45:37 +02:00
|
|
|
This PowerShell script removes any subfolder in a parent folder older than <numDays> (using last write time).
|
2023-05-26 12:20:18 +02:00
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Parameters
|
|
|
|
----------
|
2023-05-26 12:20:18 +02:00
|
|
|
```powershell
|
2023-07-29 10:15:44 +02:00
|
|
|
PS> ./remove-old-dirs.ps1 [[-path] <String>] [[-numDays] <Int32>] [<CommonParameters>]
|
2023-05-26 12:20:18 +02:00
|
|
|
|
|
|
|
-path <String>
|
2023-07-29 09:45:37 +02:00
|
|
|
Specifies the file path to the parent folder
|
2023-05-26 12:20:18 +02:00
|
|
|
|
|
|
|
Required? false
|
|
|
|
Position? 1
|
|
|
|
Default value
|
|
|
|
Accept pipeline input? false
|
|
|
|
Accept wildcard characters? false
|
|
|
|
|
|
|
|
-numDays <Int32>
|
|
|
|
Specifies the number of days (1000 by default)
|
|
|
|
|
|
|
|
Required? false
|
|
|
|
Position? 2
|
|
|
|
Default value 1000
|
|
|
|
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
|
|
|
|
-------
|
2023-05-26 12:20:18 +02:00
|
|
|
```powershell
|
|
|
|
PS> ./remove-old-dirs.ps1 C:\Temp 90
|
|
|
|
|
|
|
|
```
|
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Notes
|
|
|
|
-----
|
2023-05-26 12:20:18 +02:00
|
|
|
Author: Markus Fleschutz
|
|
|
|
|
2023-07-29 10:04:38 +02:00
|
|
|
Script Content
|
|
|
|
--------------
|
2023-05-26 12:20:18 +02:00
|
|
|
```powershell
|
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Removes old directories
|
|
|
|
.DESCRIPTION
|
2023-07-29 09:45:37 +02:00
|
|
|
This PowerShell script removes any subfolder in a parent folder older than <numDays> (using last write time).
|
2023-05-26 12:20:18 +02:00
|
|
|
.PARAMETER path
|
2023-07-29 09:45:37 +02:00
|
|
|
Specifies the file path to the parent folder
|
2023-05-26 12:20:18 +02:00
|
|
|
.PARAMETER numDays
|
|
|
|
Specifies the number of days (1000 by default)
|
|
|
|
.EXAMPLE
|
|
|
|
PS> ./remove-old-dirs.ps1 C:\Temp 90
|
|
|
|
.NOTES
|
|
|
|
Author: Markus Fleschutz
|
|
|
|
#>
|
|
|
|
|
|
|
|
param([string]$path = "", [int]$numDays = 1000)
|
|
|
|
|
|
|
|
try {
|
|
|
|
$stopWatch = [system.diagnostics.stopwatch]::startNew()
|
2023-07-29 09:45:37 +02:00
|
|
|
if ("$path" -eq "") { $path = Read-Host "Enter the file path to the parent folder" }
|
2023-05-26 12:20:18 +02:00
|
|
|
if (!(Test-Path -Path "$path" -PathType container)) { throw "Given path doesn't exist - enter a valid path, please" }
|
|
|
|
|
2023-07-29 09:45:37 +02:00
|
|
|
Write-Host "⏳ Removing subfolders older than $numDays days in $path..."
|
|
|
|
$folders = Get-ChildItem -path "$path" -directory
|
2023-05-26 12:20:18 +02:00
|
|
|
$numRemoved = 0
|
|
|
|
$count = 0
|
|
|
|
foreach ($folder in $folders) {
|
|
|
|
[datetime]$folderDate = ($folder | Get-ItemProperty -Name LastWriteTime).LastWriteTime
|
|
|
|
$count++
|
|
|
|
if ($folderDate -lt (Get-Date).AddDays(-$numDays)) {
|
2023-07-29 09:45:37 +02:00
|
|
|
Write-Host "($($count)) Removing old '$folder'..."
|
|
|
|
$fullPath = $folder | Select-Object -ExpandProperty FullName
|
|
|
|
Remove-Item -path "$fullPath" -force -recurse
|
2023-05-26 12:20:18 +02:00
|
|
|
$numRemoved++
|
2023-07-29 09:45:37 +02:00
|
|
|
} else {
|
|
|
|
Write-Host "($($count)) Skipping young '$folder'..."
|
2023-05-26 12:20:18 +02:00
|
|
|
}
|
2023-07-29 09:45:37 +02:00
|
|
|
}
|
2023-05-26 12:20:18 +02:00
|
|
|
[int]$elapsed = $stopWatch.Elapsed.TotalSeconds
|
2023-07-29 09:45:37 +02:00
|
|
|
"✔️ Removed $numRemoved of $count subfolders older than $numDays days in $elapsed sec"
|
2023-05-26 12:20:18 +02:00
|
|
|
exit 0 # success
|
|
|
|
} catch {
|
2023-07-29 09:45:37 +02:00
|
|
|
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
|
|
|
exit 1
|
2023-05-26 12:20:18 +02:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2024-01-25 13:58:49 +01:00
|
|
|
*(generated by convert-ps2md.ps1 using the comment-based help of remove-old-dirs.ps1 as of 01/25/2024 13:58:41)*
|