Losing brain cells over script - members is null?

Below is a script I put together:

When I run this script it’s saying the -members parameter is null? What am I doing wrong here?

$Users = Import-CSV C:\Temp\Remove.csv
$Group = “LakelandEmployees”

Foreach ($User in $Users) {

$SAN = $User.SamAccountName
$ADUser = Get-ADUser -Filter “SamAccountName -eq ‘$SAN’” -SearchBase “OU=Users,OU=Lakeland,DC=aspirion,DC=com” | Select-Object SamAccountName

Remove-ADGroupMember -Identity $Group -Members $ADUser.SamAccountName -Confirm:$false
}

shane,
Welcome to the forum. :wave:t3:

It seems like the query you run to get the individual AD user returns nothing … at least for one user. :man_shrugging:t3:

But you’re actually overcomplicating this task a lot. If you already have the sAMAccountNames in your CSV file you don’t need a loop at all.

Something like this should do the trick:

$Users = Import-CSV -Path 'C:\Temp\Remove.csv'
$Group = 'LakelandEmployees'

Remove-ADGroupMember -Identity $Group -Members $Users.sAMAccountName

For the future:

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

And since error messages are an important language feature telling you what’s wrong and sometimes even how to make it better you should always include the complete error message if there is one - formatted as code as well, please. :point_up_2:t3:

1 Like