I’m the IT Director at a school district and have a powershell script that takes a csv file and creates users from it. I’m by no means a programmer, but can generally cobble together what I need. I’ve run into an issue trying to remove old grade level groups from users. The way I did it with my limited knowledge of powershell was just to remove the prior grade level group. The issue I’m running into is if the student doesn’t exist at all, it throws an error when it gets to the Remove-ADGroupmember statement and won’t continue on to actually create the user. I currently have those lines commented out due to the issue. If a student left us and came back, the commands work fine. Is there a more efficient way to do this or at least to get the script as I’m running it to work with the Remove-ADGroupmember command? Thanks!

#Create an Active Directory user 
Function CreateUser {
  Param($AccountInfo)
  
    try
    {
        #Check to see if the user already exists
        $userFilterString = "samAccountName -like `"" + $AccountInfo['sAMAccountName'] + "`""
        #write-host "User" $AccountInfo['sAMAccountName'] "test`r"
        $user = Get-ADUser -Filter $userFilterString
        $group = "LHSStudents"
        
         #Set group based on grade level, put in by AW on 10-13-15
        if ($AccountInfo['description'] -eq '06')
        {write-Host "Setting group2 to 6thgrade"
        $group2 = "6thgrade"}
        #Remove-ADGroupmember -Identity "LESStudents" -Member $AccountInfo['sAMAccountName'] -Confirm:$False}
        
        elseif ($AccountInfo['description'] -eq '07')
        {write-Host "Setting group2 to 7thgrade"
        $group2 = "7thgrade"}
        #Remove-ADGroupmember -Identity "6thGrade" -Member $AccountInfo['sAMAccountName'] -Confirm:$False} 

        elseif ($AccountInfo['description'] -eq '08')
        {write-host "Setting group2 to 8thgrade"
        $group2 = "8thgrade"}
        #Remove-ADGroupmember -Identity "7thGrade" -Member $AccountInfo['sAMAccountName'] -Confirm:$False}  
        
        elseif ($AccountInfo['description'] -eq '09')
        {write-host "Setting group2 to 9thgrade"
        $group2 = "9thgrade"}
        #Remove-ADGroupmember -Identity 8thGrade -Member $AccountInfo['sAMAccountName'] -Confirm:$False}
        
        elseif ($AccountInfo['description'] -eq '10')
        {write-host "Setting group2 to 10thgrade"
        $group2 = "10thgrade"}
        #Remove-ADGroupmember -Identity "9thGrade" -Member $AccountInfo['sAMAccountName'] -Confirm:$False}  
       
        elseif ($AccountInfo['description'] -eq '11')
        {write-host "Setting group2 to 11thgrade"
        $group2 = "11thgrade"}
        #Remove-ADGroupmember -Identity "10thGrade" -Member $AccountInfo['sAMAccountName'] -Confirm:$False}  
       
        elseif ($AccountInfo['description'] -eq '12')
        {write-host "Setting group2 to 12thgrade"
        $group2 = "12thgrade"}
        #Remove-ADGroupmember -Identity "11thGrade" -Member $AccountInfo['sAMAccountName'] -Confirm:$False}
        #write-host "removed user from prior year group"
        
        else
        {write-host "you suck"}

        #If user not already created, create them
        write-host "checking if user exists"
        if ($user -eq $null)
        {
            write-host "Creating user account:" $AccountInfo['sAMAccountName'] "`r"
3 Spice ups

i would suggest try catch

try {
Remove-ADGroupmember -Identity "LESStudents" -Member $AccountInfo['sAMAccountName'] -Confirm:$False -ErrorAction Stop
} catch {
  Write-Host "Exception String: $($_.Exception.Message)"
 
}
2 Spice ups

Look into the switch statement

2 Spice ups

I did some similar work for a school but tackled the problem in a completely different way.

I made the student accounts with a group membership of AllStudents and set that as primary

I then went through the level and grade groups and cleared them of all members, then added the users to the group (rather than the group to the user).

The clearing of the group membership removes the group from every user who has it, and doesn’t cause any issues for anyone who didn’t.

JitenSh, your solution appears to be working for what I needed. I’m going to let things run for a few days and I’ll come back to mark the correct answer if it continues to work for me. Thanks!

1 Spice up

Jared5646, so at the beginning of your script you would remove everyone from the group and as it loops through each user in your file, add them back to the group?