1

I would like to remove all users from 60 ADGroups and I would like to show a progress bar but have not been able to figure out how to work. How can I add a progress bar to this?

#Progress parameter
$Progress = @{
    Activity = 'Counting groups in report files:'
    CurrentOperation = "Loading"
    Status = 'Collecting data'
    PercentComplete = 0
}
  
Write-Progress @Progress
$i = 0


# Import CSV
$CSVFiles = Import-csv -Path "C:\CSV\Groups.csv" -Encoding Default -Delimiter ";"


Foreach($File in $CSVFiles)
{
    $i++
    [int]$percentage = ($i / $CSVFiles.Count)*100
    $Name = $null
    $Name = $file.name
    $progress.CurrentOperation = "$name"
    $progress.Status = 'Processing file: '
    $progress.PercentComplete = $percentage
  
    Write-Progress @Progress


#Remove all members from CSVFiles
foreach ($ADGroup in $CSVFiles) {
Get-ADGroupMember -Identity $ADGroup.Groups.Trim() | ForEach-Object {Remove-ADGroupMember $ADGroup.Groups.Trim() $_ -Confirm:$False }

    }

  }
0

1 Answer 1

3

Here is a minimal example of how you can approach this, I'm not too sure if you're looking for an inner progress bar but there is no need for it, seems like you're doing it more complicated than it should be:

$CSVFiles = Import-csv -Path "C:\CSV\Groups.csv" -Delimiter ";"

$Progress = @{
    Activity = 'Removing Members from AD Groups'
    ID = 1
}

$i = 0

foreach($line in $CSVFiles)
{
    $Progress.PercentComplete = $i++ / $CSVFiles.Count * 100
    $Progress.Status = 'Removing members from {0}' -f $line.Groups

    try
    {
        Write-Progress @Progress
        $adgroup = Get-ADGroup $line.Groups.Trim() -Properties Member
        Remove-ADGroupMember -Identity $adgroup -Members $adgroup.Member -Confirm:$false
    }
    catch
    {
        Write-Warning $_.Exception.Message
    }
}
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.