Powershell - Batch Locate AD User Object > Object Tab

I have over 80 users that I need to locate their OU container name, in preparation to move them to one container.

The users are in a txt and CSV file.

Is there a PS script that will process the check and move?
Mike DiasAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

AlanConsultantCommented:
Hi Mike,

It would be easier if you could post a sample of the csv file (just perhaps three rows, and feel free to obfuscate as long as you don't change the structure of the data).

Thanks,

Alan.
0
Mike DiasAuthor Commented:
The CSV has 1 cell - A1 with the AD user's username i.e. firstname.lastname. There are over 80 such users, from A1 to A83..

You could create the same effect with cell A1 with "test.user1," "test.user2," ... "test.user3" etc. It'd be the same.

Hope this helps.
0
AlanConsultantCommented:
Okay - that format is not what I would expect for a CSV file.

Where did it come from?

Alan.
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Mike DiasAuthor Commented:
Hi Alan. The source was from a department  that assumed control of a number of users. They've only provided me with their names. I asked for more specific data and was given their usernames.
0
AlanConsultantCommented:
I think you'll need to convert it to a standard CSV format before you can use it.
0
Mike DiasAuthor Commented:
Ok. So I need a solution.

How do I script this in Powershell?
0
AlanConsultantCommented:
Hi Mike,

As I said, the first thing you need to do is fix the CSV file so it is in a more 'standard' format.

One way to check that is to open it in Excel (if you have that available), and see if it comes out 'nicely'.

What you posted above definitely does not.  The first element has a quote mark in front of it, and the subsequent elements have a leading space.  Those will cause PowerShell to fail to find the users, since the identifier won't match.

If you have Excel, open the attached - see how it comes out?

Alan.
EE-29065283-SampleData.csv
0
Mike DiasAuthor Commented:
Hi Alan.

The CSV has 1 cell - A1 with the AD user's username i.e. firstname.lastname

There are no inverted commas, or spaces before or after. That was an expression I used above. The cell A1 is test.user1 cell A2 is test.user2 and so on.
0
AlanConsultantCommented:
Hi Mike,

I copied what you posted:

"test.user1," "test.user2," ... "test.user3"

I took out the three dots as I assumed you meant it went on in the same format, and then created a CSV file using that format as attached (just re-did it again in case I made a mistake before).

If you open this in Excel, do you see the leading quote mark, and the leading spaces in each cell from the second one on?

Maybe I am missing something obvious here, but it does not appear to be a standard CSV format to me, and hence I cannot see how PowerShell will be able to parse it and match to actual usernames.


Alan.
EE-29065283-SampleData.csv
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Having a single column in a file makes it just a text file ;-). A CSV has more than one column.
We can treat both formats easily. What we need to know is whether there is a header line or not.
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Assuming a simple text file with no header:
Get-Content Users.txt | % { Get-ADUser $_ } | select SamAccountName, DistinguishedName

Open in new window

1

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
AlanConsultantCommented:
@Qlemo:

Does that work for you with the sample data file above (renamed as users.txt)?


Alan.
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Certainly not with your last one, but that has been indicated as not being correct anyway.
0
AlanConsultantCommented:
Hi Qlemo,

The OP specifically said that *is* the format of the CSV file.

What makes you say otherwise?

Thanks,

Alan.
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
#a42347377 says so, Alan. It's just a file with one name per line.
0
AlanConsultantCommented:
Okay - I'm going to leave this guy with you.

Seems like they went out of their way to make things difficult for no good reason.

Good luck.

Alan.
0
Mike DiasAuthor Commented:
Concise answer. Simple to use.
0
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.