Merge branch 'main' of github.com:fleschutz/PowerShell

This commit is contained in:
Markus Fleschutz 2025-03-19 12:36:28 +01:00
commit ae470e0596
2 changed files with 7 additions and 7 deletions

View File

@ -5,7 +5,7 @@
This PowerShell script changes the working directory to the PowerShell scripts folder. This PowerShell script changes the working directory to the PowerShell scripts folder.
.EXAMPLE .EXAMPLE
PS> ./cd-scripts.ps1 PS> ./cd-scripts.ps1
📂C:\Repos\PowerShell\scripts 📂C:\Repos\PowerShell\scripts entered (has 644 scripts).
.LINK .LINK
https://github.com/fleschutz/PowerShell https://github.com/fleschutz/PowerShell
.NOTES .NOTES

View File

@ -9,7 +9,9 @@
Specifies the number of days (1000 by default) Specifies the number of days (1000 by default)
.EXAMPLE .EXAMPLE
PS> ./remove-old-dirs.ps1 C:\Temp 365 PS> ./remove-old-dirs.ps1 C:\Temp 365
Removed 0 subfolders in 1s (67 skipped). Scanning C:\Temp for subfolders older than 365 days...
Removing old 'TestFolder'...
Removed 1 of 49 subfolders in 1s.
.LINK .LINK
https://github.com/fleschutz/PowerShell https://github.com/fleschutz/PowerShell
.NOTES .NOTES
@ -24,21 +26,19 @@ try {
if (!(Test-Path -Path "$path" -PathType container)) { throw "Given path doesn't exist - enter a valid path, please" } if (!(Test-Path -Path "$path" -PathType container)) { throw "Given path doesn't exist - enter a valid path, please" }
Write-Host "⏳ Searching in '$path' for subfolders older than $numDays days..." Write-Host "⏳ Searching in '$path' for subfolders older than $numDays days..."
$numRemoved = $numSkipped = 0 $numRemoved = 0
$folders = Get-ChildItem -path "$path" -directory $folders = Get-ChildItem -path "$path" -directory
foreach ($folder in $folders) { foreach ($folder in $folders) {
[datetime]$folderDate = ($folder | Get-ItemProperty -Name LastWriteTime).LastWriteTime [datetime]$folderDate = ($folder | Get-ItemProperty -Name LastWriteTime).LastWriteTime
if ($folderDate -lt (Get-Date).AddDays(-$numDays)) { if ($folderDate -lt (Get-Date).AddDays(-$numDays)) {
Write-Host "Removing old '$folder'..." Write-Host "Removing old '$folder'..."
$fullPath = $folder | Select-Object -ExpandProperty FullName $fullPath = $folder | Select-Object -ExpandProperty FullName
Remove-Item -path "$fullPath" -force -recurse Remove-Item -path "$fullPath" -force -recurse
$numRemoved++ $numRemoved++
} else {
$numSkipped++
} }
} }
[int]$elapsed = $stopWatch.Elapsed.TotalSeconds [int]$elapsed = $stopWatch.Elapsed.TotalSeconds
"✅ Removed $numRemoved subfolders in $($elapsed)s ($numSkipped skipped)." "✅ Removed $numRemoved of $($folders.Count) subfolders in $($elapsed)s."
exit 0 # success exit 0 # success
} catch { } catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"