-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemove-ItemWithProgress.psm1
36 lines (28 loc) · 1.24 KB
/
Remove-ItemWithProgress.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
function Remove-ItemWithProgress {
param(
[Parameter(Mandatory = $true)]
[System.IO.DirectoryInfo] $Path
)
try {
[string] $_Path = (Resolve-Path $Path).Path;
}
catch { throw 'Invalid Path.'; }
$RegEx = '"([^"]+)"\.';
$Activity = "Deleting $_Path"
Write-Progress -Id 0 -Activity $Activity -Status "Reading files..." -PercentComplete 0;
$Directory = Get-Location
$ItemCount = (Get-ChildItem -Path $_Path -Recurse -Force).Count + 1; # Needs to include -Force for hidden directories
$Counter = 0;
$Start = (Get-Date)
Remove-Item -Path $_Path -Recurse -Force -Verbose 4>&1 | ForEach-Object {
$Counter++;
$Elapsed = ((Get-Date) - $Start).TotalSeconds
$Remaining = ($Elapsed / ($Counter / $ItemCount)) - $Elapsed
$Text = [regex]::Match($_, $RegEx).Captures.Groups[1].Value.Replace($Directory, ".");
$Progress = $Counter * 100 / $ItemCount
Write-Progress -Id 0 -Activity $Activity -Status $Text -PercentComplete $Progress -SecondsRemaining $Remaining
};
Write-Progress -Id 0 -Activity $Activity -PercentComplete 100 -Completed;
Write-Output "Deleted $Counter items."
}
Export-ModuleMember -Function Remove-ItemWithProgress