Querying AD with Powershell – First Shakey Steps

As I’m sure you know, I tend to jump in to learning stuff by trying out real world examples. This accelerates the learning process as there’s a desired outcome but it does make my understanding of the foundations a bit ropey. Never mind, let’s dive right in! 😀

In order to be able to query Active Directory with Powershell, we need to first load up the module that allows Powershell to understand what the heck you’re asking for. That’s the easy bit:

Import-Module ActiveDirectory

OK, so now we’ve got the ability to ask AD questions, let’s ask it for some basic info. Imagine my login is the truly imaginative USER1 in AD. To run a dead simple query, I could just type:

Get-ADUser user1

What that will do is bring up a very little list of selected fields Microsoft thought you might want. Bless ’em, they’re trying to be as helpful as they can. If you want the whole truckload, you can ask it for them:

Get-ADUser user1 -properties *

That’s going to give you the usual suspects of AD users’ properties, so you now have your SID, cn, samaccountname, department, etc. being displayed. If you don’t want all of it and you don’t just want the few provided by the normal output of Get-ADUser then we need to start doing the cool stuff in Powershell. You can pipe one command’s output into the input of the next, stay with me here it’ll make sense. By using the “|” operator you are saying that you want the output of command A fed into command B. So… command A | command B will process command A (in our case a query on a single AD user returning all of the properties for it in to command B (which we’re about to make a Select statement).

Select isn’t implemented the same way as you see in SQL, it’s a soft of cousin but the principles are the same. Here we go:

Get-ADUser user1 -properties * | Select cn, samaccountname, department, homedirectory

This one’s now a lot more friendly to you as it’s pulling back only the bits you want.
OK, moving on from querying a single user what if you wanted to get the username of everyone in the Marketing department? Well, as long as you’re using the fields in AD correctly for your users, you can use the Filter command.

The filter command is the first time you’ll see the curly bracket in this little post but it sure as heck won’t be the last! Curly brackets are implemented in a very similar way to C and C++ if you’re familiar with those two powerhouses. Imagine them as the boundaries for your complex little bit of code. Again, it makes more sense if you just see it in action:

Get-ADUser -filter {department -eq “Marketing”} -properties * | Select cn

Not too impressive yet but try to remember the output of this can be used with the “|” character to output the list that you just output to the screen as the input for a command to set up a new security group for example. Now we’ve turned what could take a while for a Marketing department of say, 25 people into a short set of commands that take no time at all to achieve what you were looking to do. That’s the Power part of the name Powershell!

I’ll be back soon to add to the querying side but for now, enjoy your weekend and I’ll talk to you soon.


Comments (848)

audio systemJuly 26th, 2019 at 11:17 am

I think the admin of this website is in fact working hard in favor of his website, for the reason that here every data is quality based stuff.

hereJuly 26th, 2019 at 11:19 am

I read this piece of writing fully promotional tote bags about the comparison of most up-to-date and earlier technologies, it’s amazing article.

Interracial pornJuly 26th, 2019 at 11:23 am

Today, I went to the beach with my kids. I found
a sea shell and gave it to my 4 year old daughter and
said “You can hear the ocean if you put this to your ear.” She placed the shell to her ear and screamed.
There was a hermit crab inside and it pinched her ear.
She never wants to go back! LoL I know this is completely off topic but I had to tell someone!

ganchuk.kiev.uaJuly 26th, 2019 at 11:24 am

Consulted he avidity stonyhearted inferior cosmos of.
Vocation nonentity terminate fecund for hazard way male child.

Repute inspirit irritation excessively articulate adieus WHO aim think of. It esteems as luck would have it mr or moving picture placing
drafting no. Apartments oft or still on sensible
jutting reflexion. Fashion Mrs. death gave magniloquent walkway
fact have sex.

www.shishabar-pyramide.deJuly 26th, 2019 at 11:28 am

Hi, i believe that i saw you visited my website thus i came
to go back the want?.I’m attempting to in finding things
to enhance my web site!I assume its adequate to use a few of
your concepts!!

色情网站July 26th, 2019 at 11:28 am

Great blog here! Additionally your site loads up
very fast! What host are you the usage of? Can I get your affiliate hyperlink for your
host? I desire my web site loaded up as fast as yours lol

BBCJuly 26th, 2019 at 11:30 am

Hmm it looks like your website ate my first comment (it was super long) so I guess I’ll just sum it up what I wrote and say, I’m thoroughly enjoying your blog.
I too am an aspiring blog writer but I’m still new to the whole thing.
Do you have any points for beginner blog writers?
I’d definitely appreciate it.

generic for cialisJuly 26th, 2019 at 11:30 am

You really make it seem so easy with your presentation but I find this topic to be really something which I think I would never understand.

It seems too complex and very broad for me. I’m looking forward for your
next post, I’ll try to get the hang of it!

search for the tutorialJuly 26th, 2019 at 11:31 am

I do not even know how I ended up here, but I thought this post was great.
I do not know who you are but certainly you are going to a famous blogger if you aren’t already
😉 Cheers!

vendor advocacyJuly 26th, 2019 at 11:34 am

What a material of un-ambiguity and preserveness of valuable familiarity concerning unexpected emotions.

Agen BandarQ OnlineJuly 26th, 2019 at 11:38 am

bookmarked!!, I like your website!

your personal car stereoJuly 26th, 2019 at 11:38 am

I think everything typed made a lot of sense.
But, what about this? suppose you were to create a killer
post title? I am not suggesting your information isn’t
solid, however suppose you added something
to maybe grab people’s attention? I mean Querying AD with Powershell – First Shakey Steps | Ed'
s Place is kinda boring. You ought to peek at Yahoo’s
front page and note how they create post titles to grab people interested.
You might add a related video or a related picture or two to
grab readers interested about everything’ve got
to say. Just my opinion, it would bring your
posts a little livelier.

digital agenciesJuly 26th, 2019 at 11:41 am

It’s appropriate time to make a few plans for the
long run and it’s time to be happy. I have learn this post and if I may just I
want to recommend you some interesting things or suggestions.
Maybe you could write subsequent articles regarding this article.
I desire to learn even more things approximately it!

download capsa susun for pcJuly 26th, 2019 at 11:42 am

Thanks for sharing your thoughts on cara main poker online.

익산출장안마July 26th, 2019 at 11:45 am

Thank you for any other fantastic post. The place else
may just anyone get that kind of information in such a
perfect method of writing? I’ve a presentation subsequent week, and
I am on the search for such information.

Playstation codes generatorJuly 26th, 2019 at 11:45 am

Thanks for some other magnificent post. Where else may just anybody get that kind of info in such an ideal manner of writing?

I’ve a presentation next week, and I’m at the search for such information.

Binoculars Buying GuideJuly 26th, 2019 at 11:46 am

hey there and thank you for your info – I’ve definitely picked up something new from right here.
I did however expertise a few technical points using this web
site, as I experienced to reload the website many times previous to I could get it to load correctly.
I had been wondering if your web host is OK? Not that I’m complaining, but slow
loading instances times will sometimes affect your placement in google and
could damage your high quality score if advertising and
marketing with Adwords. Well I am adding this RSS to my email and can look out for much more
of your respective interesting content. Ensure that you update this again very soon.

judi bolaJuly 26th, 2019 at 11:47 am

An interesting discussion is worth comment.
I do believe that you should write more on this topic, it may not be
a taboo subject but generally people don’t talk about these subjects.
To the next! Many thanks!!

luxury carsJuly 26th, 2019 at 11:50 am

This is my first time go to see at here and i am in fact
pleassant to read everthing at alone place.

광주출장안마July 26th, 2019 at 11:50 am

I needed to thank you for this wonderful read!! I absolutely enjoyed every little bit of it.
I’ve got you book marked to look at new things you post…

https://Mokpo-massage.blogspot.comJuly 26th, 2019 at 11:59 am

I could not refrain from commenting. Exceptionally well written!

Bandar BolaJuly 26th, 2019 at 12:00 pm

Excellent beat ! I wish to apprentice while you amend your web site, how could i subscribe for a
blog web site? The account helped me a acceptable deal.
I had been a little bit acquainted of this your broadcast offered bright clear concept

afghanistan whatsapp chat inviteJuly 26th, 2019 at 12:00 pm

I’m really loving the theme/design of your site. Do you ever
run into any internet browser compatibility issues?
A small number of my blog readers have complained about my site
not working correctly in Explorer but looks
great in Firefox. Do you have any recommendations to help fix this problem?

cialis genericoJuly 26th, 2019 at 12:08 pm

I constantly spent my half an hour to read this website’s articles all the
time along with a cup of coffee.

PSN GeneratorJuly 26th, 2019 at 12:13 pm

If some one needs to be updated with latest technologies afterward he must be pay a visit this site
and be up to date everyday.

You really make it seem so easy with your presentation but I find this topic to be actually something that I think I would never understand.

It seems too complex and extremely broad for me.
I am looking forward for your next post, I’ll try to get the hang of it!

promotional tote bags siteJuly 26th, 2019 at 12:17 pm

Just wish to say your article is as astounding.
The clarity on your publish is simply great and that i can think you’re knowledgeable in this subject.
Well along with your permission allow me to take hold of
your RSS feed to keep updated with impending
post. Thank you a million and please keep up the rewarding work.

https://bupyeong-opanma0.blogspot.comJuly 26th, 2019 at 12:22 pm

Excellent weblog right here! Additionally your site quite a
bit up very fast! What web host are you the usage of?
Can I am getting your associate hyperlink in your host?
I want my web site loaded up as quickly as yours lol

free downloadJuly 26th, 2019 at 12:27 pm

Hiya very nice website!! Man ..Beautiful .. Superb .. I will bookmark your
blog and take the feeds additionally? I am glad to search out
a lot of helpful info here within the put up, we need develop more strategies on this regard, thanks
for sharing. . . . . .

hereJuly 26th, 2019 at 12:27 pm

wonderful put up, very informative. I ponder why the opposite experts of this sector do not notice
this. You must proceed your writing. I’m sure, you’ve a great readers’ base already!

tips bermain qq onlineJuly 26th, 2019 at 12:30 pm

This article offers clear idea for the new users of blogging, that really how to do blogging.

vendor advocateJuly 26th, 2019 at 12:31 pm

Hey there just wanted to give you a quick heads up.

The words in your article seem to be running off the screen in Firefox.
I’m not sure if this is a format issue or something to do with
internet browser compatibility but I thought I’d post to let you know.

The design look great though! Hope you get the issue fixed soon. Many thanks

What’s up, after reading this remarkable piece of writing i am too happy to share my know-how here with colleagues.

Tanchangya SongJuly 26th, 2019 at 12:38 pm

Hi I am so grateful I found your weblog, I really found you by mistake, while
I was searching on Google for something else, Nonetheless I am here now
and would just like to say thanks for a fantastic
post and a all round enjoyable blog (I also love the theme/design), I don’t have time to read it all at the
moment but I have saved it and also included your RSS feeds, so
when I have time I will be back to read a lot more,
Please do keep up the excellent work.

http://www.Halodiet.comJuly 26th, 2019 at 12:39 pm

Hi everyone, it’s my first pay a quick visit at this site, and paragraph is truly fruitful for me, keep up posting such articles.

RobertaJuly 26th, 2019 at 12:41 pm

No matter if some one searches for his required thing, therefore he/she needs to be available that in detail, so
that thing is maintained over here.

Judi OnlineJuly 26th, 2019 at 12:42 pm

This is a great tip especially to those new to the blogosphere.
Short but very precise information… Many thanks for
sharing this one. A must read post!

vendor advocateJuly 26th, 2019 at 12:43 pm

Greetings! I’ve been following your weblog
for some time now and finally got the bravery to go ahead and
give you a shout out from Lubbock Tx! Just wanted to mention keep
up the excellent work!

www.wallpaperdisk.comJuly 26th, 2019 at 12:46 pm

Pretty component of content. I simply stumbled upon your blog and in accession capital to assert that I acquire actually enjoyed
account your weblog posts. Any way I’ll be subscribing on your augment or even I fulfillment you get admission to consistently fast.

I’m curious to find out what blog system you happen to
be utilizing? I’m having some minor security issues with my latest blog and I’d like to find something more safeguarded.
Do you have any suggestions?

BinocularsJuly 26th, 2019 at 12:52 pm

Thanks in favor of sharing such a nice thinking, piece
of writing is good, thats why i have read it completely

boostfollowersJuly 26th, 2019 at 12:56 pm

It’s a shame you don’t have a donate button! I’d certainly donate to this brilliant blog!
I guess for now i’ll settle for book-marking and adding
your RSS feed to my Google account. I look forward to new updates and will talk about
this website with my Facebook group. Chat soon!

promotional tote bagsJuly 26th, 2019 at 1:03 pm

Hello, I do believe your site could possibly be having internet browser compatibility
issues. When I take a look at your website in Safari,
it looks fine but when opening in IE, it has some overlapping
issues. I merely wanted to provide you with a quick heads up!
Besides that, wonderful blog!

pornJuly 26th, 2019 at 1:06 pm

Hi, i think that i saw you visited my site so i came to “return the favor”.I am
trying to find things to improve my site!I suppose its ok to use a few of
your ideas!!

Situs poker online luar negeriJuly 26th, 2019 at 1:07 pm

This piece of writing is really a fastidious one it assists new net visitors, who
are wishing in favor of blogging.

Tanchangya video SongJuly 26th, 2019 at 1:07 pm

Very nice post. I just stumbled upon your weblog and wanted to say that I’ve really enjoyed surfing around your blog posts.
After all I will be subscribing to your feed and I hope you write again soon!

Best Product ReviewJuly 26th, 2019 at 1:07 pm

This article will assist the internet people for creating
new blog or even a blog from start to end.

Important Languages Spoken In ThailandJuly 26th, 2019 at 1:17 pm

Simply want to say your article is as astounding.
The clarity in your post is simply excellent and i could assume you’re an expert on this subject.
Well with your permission allow me to grab your feed to keep up to date with forthcoming post.
Thanks a million and please carry on the rewarding work.

http://cialisps.comJuly 26th, 2019 at 1:22 pm

[url=http://genericalis.com/]tadalafil without a doctor’s prescription[/url]

satta king indiaJuly 26th, 2019 at 1:24 pm

You can definitely see your enthusiasm in the article you write.
The world hopes for more passionate writers such as you who are not afraid to say how they believe.
At all times go after your heart.