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
LVL 15
Naser GabajE&P Senior Software SpecialistAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jacob DurhamIT Support Analyst II (Lead Infrastructure Engineer)Commented:
What do you have so far? And what is the formatting of the CSV? Is it like below?

userid
----------
user1
user2
Naser GabajE&P Senior Software SpecialistAuthor Commented:
yes. exactly
Jacob DurhamIT Support Analyst II (Lead Infrastructure Engineer)Commented:
Here's a start

$users = "jacobd","talonssss"

foreach ($user in $users)
 {
    try {
        $ADUser = Get-ADUser -Identity $user -Properties primarygroup -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 ", ")."
}

Open in new window


Output

User 'Jacobd' exists and his primary group is Domain Users.
User 'talonssss' does not exist.

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Naser GabajE&P Senior Software SpecialistAuthor Commented:
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?
Jacob DurhamIT Support Analyst II (Lead Infrastructure Engineer)Commented:
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 GabajE&P Senior Software SpecialistAuthor Commented:
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 DurhamIT Support Analyst II (Lead Infrastructure Engineer)Commented:
Give me the entirety of the code.

You're not piping anything into the identity parameter which means one of the variables is off.
Naser GabajE&P Senior Software SpecialistAuthor Commented:
$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 McCartneySYS ADMINISTR I INFRASCommented:
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 GabajE&P Senior Software SpecialistAuthor Commented:
Thank you Jacob for your help and I apologize for the delay to reply to you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.