Updated cd-scripts.ps1 and remove-old-dirs.ps1

This commit is contained in:
Markus Fleschutz 2025-03-19 08:53:19 +01:00
parent 225a1ae902
commit d651aad0f8
2 changed files with 8 additions and 8 deletions

View File

@ -5,7 +5,7 @@
This PowerShell script changes the working directory to the PowerShell scripts folder.
.EXAMPLE
PS> ./cd-scripts.ps1
📂C:\Repos\PowerShell\scripts
📂C:\Repos\PowerShell\scripts entered (has 644 scripts).
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
@ -16,7 +16,7 @@ try {
$path = Resolve-Path "$PSScriptRoot"
if (-not(Test-Path "$path" -pathType container)) { throw "PowerShell scripts folder at 📂$path doesn't exist (yet)" }
$files = Get-ChildItem $path -attributes !Directory
"📂$path entered, containing $($files.Count) scripts."
"📂$path entered (has $($files.Count) scripts)."
Set-Location "$path"
exit 0 # success
} catch {

View File

@ -9,7 +9,9 @@
Specifies the number of days (1000 by default)
.EXAMPLE
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
https://github.com/fleschutz/PowerShell
.NOTES
@ -24,21 +26,19 @@ try {
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..."
$numRemoved = $numSkipped = 0
$numRemoved = 0
$folders = Get-ChildItem -path "$path" -directory
foreach ($folder in $folders) {
[datetime]$folderDate = ($folder | Get-ItemProperty -Name LastWriteTime).LastWriteTime
if ($folderDate -lt (Get-Date).AddDays(-$numDays)) {
Write-Host "Removing old '$folder'..."
Write-Host "Removing old '$folder'..."
$fullPath = $folder | Select-Object -ExpandProperty FullName
Remove-Item -path "$fullPath" -force -recurse
$numRemoved++
} else {
$numSkipped++
}
}
[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
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"