Check if the users are part of AD groups

Hello everyone!

Greetings !

I have an excel file with a worksheet containing columns:
(A) Application Name
(B) Ad Groups
(C) Users

I would like to check if the users mentioned are part of the ADgroups mentioned

Here the data format is like below:

App Name ----------AD groups ---------------------- Users
App1 ------------------ Group1: Group2 ---------- User1: user11;User101
App1 ----------------- Group2;Group10 -------------- User22;User200

Users should be checked if they are not part of ADgroups

Result expected is:
User1 is not part of Group2
User101 is not part of Group101
User200 is not part of Group10.

Is there possiblity that I can check this with PowerShell ? Please advise… Thank you !

Usually it is. :wink: But it will be way easier when your input data comes in a consistent format. Better than an Excel-Sheet would be a CSV file with only one data set per line and per cell. :wink:
You could use the following cmdlets to get the inforemation you’re after:

BTW: 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 <---- Click :point_up_2:t4: :wink:

Hello @Olaf ,

I came up with below, but since the users in file is actually also had empty spaces as “;;”, but am unable to exclude them from output as I am getting output like below:

 not exists in the group
$csv = ipcsv -Path .\Desktop\abc.csv
foreach($entry in $csv){

$userList = $entry.Users
$users = $userList.split(";")


$groupList = $entry.AssociatedADGroup
$groups = $groupList.split(";")




foreach($group in $groups){
$group

$members = Get-ADGroupMember -Identity $group -Recursive | Select -ExpandProperty Name

    ForEach ($user in $users) {

    If($user -ne $null){
            Write-Verbose "Checking for $user in $group"
            If ($members -contains $user) {
              Write-Host "$user exists in the group $group"
                } 
            Else {
              Write-Host "$user not exists in the group $group"
                }

              }
            }
        }

}




Since you do not share some sample input data it’s hard to recommend something meaningful. I’d recommend in general to use the sAMAccountName instead of the name of the user object as it is unique and usually does not contain spaces.