Avatar of Naser Gabaj
Naser Gabaj
Flag for United States of America asked on

Need powershell script

I have excel csv file that has one column (User ID) in OU in Active directory, I need to create powershell script that going to check and verify on each one of them if they are already exist in Active directory or not and if it's exist write the group name that he belong to
PowershellActive Directory

Avatar of undefined
Last Comment
Naser Gabaj

8/22/2022 - Mon
Jacob Durham

What do you have so far? And what is the formatting of the CSV? Is it like below?

userid
----------
user1
user2
Naser Gabaj

ASKER
yes. exactly
ASKER CERTIFIED SOLUTION
Jacob Durham

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Naser Gabaj

ASKER
to be able to use my csv file as an input to this script I should replace the part where it says "$users = "jacobd","talonssss"" to be :
Import-Csv -Path "Y:\MissingMembers.csv" | ForEach-Object {<YOUR SCRIPT>}...right?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Jacob Durham

Yes
$users = import-csv c:\path\to\file.csv

Open in new window



You might need to use $user.userid depending on how the CSV looks.
Naser Gabaj

ASKER
I feel I'm almost there but the result is not as I expect, please see attached, is there a way I can direct my search to look into a specific OU name?

Should I replace the part
" $ADUser = Get-ADUser -Identity $user -Properties primarygroup -ErrorAction Stop"
with the
" $ADUser = Get-ADUser -Identity $user -Properties OU -SearchBase 'OU=XYZ' -ErrorAction Stop"

Please confirm
2018-10-23_15-01-31.jpg
Jacob Durham

Give me the entirety of the code.

You're not piping anything into the identity parameter which means one of the variables is off.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Naser Gabaj

ASKER
$users = import-csv R:\Groups\Members.csv
foreach ($user in $users)
 {
    try {
        $ADUser = Get-ADUser -Identity $user -Properties OU -SearchBase 'OU=<interestedGroup>,DC=<DOMAIN>,DC=com' -ErrorAction Stop
    }
    catch {
        if ($_ -like "*Cannot find an object with identity: '$user'*") {
            "User '$user' does not exist."
        }
        else {
            "An error occurred: $_"
        }
        continue
    }
    "User '$($ADUser.SamAccountName)' exists and his primary group is $(($aduser.Primarygroup -replace "CN=|,.*") -join ", ")."
}
Timothy McCartney

As Jacob mentioned, you need to use $user.userid. Like this:
$ADUser = Get-ADUser $user.userid ...

Open in new window


IF your CSV file's header is actually 'User ID' then you will need to use $user."User ID"
$ADUser = Get-ADUser $user."User ID" ...

Open in new window

Naser Gabaj

ASKER
Thank you Jacob for your help and I apologize for the delay to reply to you.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck