mirror of
https://github.com/fleschutz/PowerShell.git
synced 2025-04-22 18:08:18 +02:00
Merge branch 'main' of github.com:fleschutz/PowerShell
This commit is contained in:
commit
ae470e0596
@ -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
|
||||
|
@ -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])"
|
||||
|
Loading…
Reference in New Issue
Block a user