Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, November 8, 2012 10:23 AM
Hi there, I need a simple Powershell script to delete large folders and provide a simple progress bar. I've written the following .PS1 file which isn't working:
Remove-Item "C:/MyFolder"
-PercentComplete
I know it's a simple one but I'm new to Powershell. If anyone can help that would be great. Thanks.
All replies (3)
Thursday, November 8, 2012 10:44 AM ✅Answered
Here is a sample script for if you would like to display a progress bar for file deletion in the program files folder:
$ListOfFiles = Get-ChildItem -Path 'C:\Program Files' -Recurse | Where-Object {!$_.PSIsContainer}
for ($i = 1; $i -lt $ListOfFiles.count; $i++) {
Write-Progress -Activity 'Deleting files...' -Status $ListOfFiles[$i].FullName -PercentComplete ($i / ($ListOfFiles.Count*100))
Remove-Item -Path $ListOfFiles[$i].FullName -WhatIf
}
Jaap Brasser
http://www.jaapbrasser.com
Thursday, November 8, 2012 11:14 PM ✅Answered
PowerTip of the Day, from PowerShell.com: |
Adding Progress to Long-Running CmdletsSometimes cmdlets take some time, and unless they emit data, the user gets no feedback. Here are three examples for calls that take a long time without providing user feedback: $hotfix = Get-Hotfix $products = Get-WmiObject Win32_Product $scripts = Get-ChildItem $env:windir *.ps1 -Recurse -ea 0 To provide your scripts with better user feedback, here's a function called Start-Progress. It takes a command and then executes it in a background thread. The foreground thread will output a simple progress indicator. Once the command completes, the results are returned to the foreground thread. It's really simple and can make a heck of a difference to the user: $hotfix = Start-Progress {Get-Hotfix} $products = Start-Progress {Get-WmiObject Win32_Product} $scripts = Start-Progress {Get-ChildItem $env:windir *.ps1 -Recurse -ea 0} And here's the function code you need: function Start-Progress { param( [ScriptBlock] $code ) $newPowerShell = [PowerShell]::Create().AddScript($code) $handle = $newPowerShell.BeginInvoke() while ($handle.IsCompleted -eq $false) { Write-Host '.' -NoNewline Start-Sleep -Milliseconds 500 } Write-Host '' $newPowerShell.EndInvoke($handle) $newPowerShell.Runspace.Close() $newPowerShell.Dispose() } |
Mike Crowley | MVP
My Blog -- Planet Technologies
Tuesday, November 13, 2012 1:54 AM
Hi,
Just checking in to see if the suggestions were helpful. Please let us know if you would like further assistance.
If you are TechNet Subscription user and have any feedback on our support quality, please send your feedback here.
Yan Li
TechNet Community Support