0

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

2
  • You can use write-progress Commented Apr 20, 2021 at 12:26
  • how would i go about adding write-progress to the command, as this command in its self is recursive, the only documentation for this is with it in loops of some form Commented Apr 20, 2021 at 12:34

1 Answer 1

1

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.