Reputation: 321
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 }
}
}
Upvotes: 1
Views: 758
Reputation: 61213
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
}
}
Upvotes: 3