stuart harper
stuart harper

Reputation: 33

How to add a progress bar to a command in PowerShell

Im writing a script that removes user profiles with in powershel using this command

Get-WMIObject -class Win32_UserProfile | Where {(!$_.Special) -and (!$_.loaded)} | Remove-WmiObject

How would i go about adding a progress bar to the command, so that when one profile has been removed it would go up type of thing, this is part of a gui program and the console is hidden

Upvotes: 0

Views: 560

Answers (1)

MF-
MF-

Reputation: 185

I would suggest using a loop to go through each user profile if you're going to use Write-Progress. Something along the lines of.

$profiles = Get-WMIObject -class Win32_UserProfile | Where {(!$_.Special) -and (!$_.loaded)}
$num_profiles = $profiles.Count
Function Remove_Prof
{
    for ($i = 1; $i -lt $num_profiles; $i++)
        { 
        Remove-WMIObject $profiles[$i]
        Start-Sleep -m 1000  
        Write-Progress -Activity 'Removing Profiles' -Status "Deleted $i out of $num_profiles profiles" -PercentComplete (($i/$num_profiles) * 100)
        
        }
}

Remove_Prof;

You can replace/remove the sleep call - I just had that because in my test I didn't actually delete the profile(s).

Upvotes: 1

Related Questions