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.

Share

Comments (848)

WesleyJepleJuly 16th, 2019 at 9:25 am

viagra no prescription cheap
buy sildenafil

WesleyJepleJuly 23rd, 2019 at 8:53 pm

viagra vs levitra
viagra 100mg pills for sale

Situs Poker OnlineJuly 25th, 2019 at 2:55 pm

Definitely consider that which you said. Your favorite justification seemed to be at the internet the simplest
thing to take into accout of. I say to you, I
certainly get irked at the same time as people consider worries that they just
do not recognise about. You controlled to hit the nail upon the highest
and also outlined out the whole thing without having side effect , other people can take a signal.
Will likely be again to get more. Thank you

spela på svenska casinon med bank IDJuly 25th, 2019 at 2:57 pm

Hurrah, that’s what I was seeking for, what a data!

present here at this blog, thanks admin of this site.

poker Online indonesiaJuly 25th, 2019 at 3:25 pm

Just desire to say your article is as amazing. The clarity in your put up is simply spectacular and i could think you’re an expert on this
subject. Well together with your permission allow
me to clutch your RSS feed to stay updated with coming near
near post. Thanks a million and please continue the gratifying work.

Good day! I know this is kinda off topic but I was wondering which
blog platform are you using for this website? I’m getting fed
up of WordPress because I’ve had problems with hackers and
I’m looking at options for another platform. I would be fantastic if you could point me in the direction of a good platform.

원더풀게임July 25th, 2019 at 4:04 pm

I am regular reader, how are you everybody? This paragraph posted
at this web page is in fact nice.

lekarskie,July 25th, 2019 at 4:06 pm

What’s up to every one, because I am genuinely keen of reading this weblog’s post to be updated regularly.
It includes nice stuff.

pratamafiberglass.comJuly 25th, 2019 at 4:33 pm

is a trusted QQ Casino poker Online and Bandar Ceme Online wagering
website that provides on the internet card video games such as
Online Poker, DominoQQ, Capsa Online, Ceme Online, Ceme99, Online Betting Online Poker
Sites. QQ Texas Hold’em Ceme, the best as well as best online texas hold’em representative
website with 24-hour IDN Online Poker service. For
those of you Lovers of the video game Online Online and also who wish to play gambling Online Poker, Online Ceme, Domino QQ, City Ceme,
Online Gaming, Bandar Capsa Online in 1 ID

two-moviesJuly 25th, 2019 at 4:46 pm

I have read so many content concerning the blogger lovers but
this article is truly a pleasant article, keep it up.

dewi365July 25th, 2019 at 5:17 pm

This post will help the internet users for setting
up new blog or even a weblog from start to end.

poker online uang asli tanpa modalJuly 25th, 2019 at 5:28 pm

Hello there! Quick question that’s completely off
topic. Do you know how to make your site mobile friendly?
My site looks weird when viewing from my apple iphone.

I’m trying to find a theme or plugin that might be able to fix this problem.

If you have any suggestions, please share.
Many thanks!

pornJuly 25th, 2019 at 5:34 pm

Hey there! Would you mind if I share your blog with my facebook group?
There’s a lot of folks that I think would really enjoy your content.
Please let me know. Thank you

poker onlineJuly 25th, 2019 at 5:34 pm

It’s very straightforward to find out any topic on net as
compared to books, as I found this paragraph at
this site.

Situs Pokerv TerbaikJuly 25th, 2019 at 5:44 pm

Hi there everyone, it’s my first pay a visit at this web page, and piece of writing is
really fruitful designed for me, keep up posting such posts.

제휴없는사이트July 25th, 2019 at 5:57 pm

At this time it looks like Expression Engine is the best blogging platform available right now.
(from what I’ve read) Is that what you’re using on your blog?

poker Online uang asli tanpa modalJuly 25th, 2019 at 5:58 pm

Hello! I could have sworn I’ve been to this website before but after checking through some of the post I realized it’s new to me.
Anyways, I’m definitely glad I found it and I’ll be bookmarking and checking back frequently!

ocean kingJuly 25th, 2019 at 6:03 pm

Oh my goodness! Impressive article dude! Thank you,
However I am having difficulties with your RSS. I don’t understand the reason why I cannot subscribe to
it. Is there anybody getting similar RSS issues? Anybody who
knows the solution can you kindly respond? Thanks!!

dominoqqJuly 25th, 2019 at 6:06 pm

This page truly has all the info I wanted about this
subject and didn’t know who to ask.

googleJuly 25th, 2019 at 6:08 pm

Hi there to every one, it’s truly a pleasant for me to pay a visit this site, it includes useful Information.

RapCoreJuly 25th, 2019 at 6:09 pm

Interesting blog! Is your theme custom made or did you download it from somewhere?
A design like yours with a few simple tweeks would really make my blog stand out.
Please let me know where you got your theme. Many thanks

skillsmässaJuly 25th, 2019 at 6:10 pm

This post is really a fastidious one it assists new
web users, who are wishing for blogging.

chelating agentJuly 25th, 2019 at 6:11 pm

Good article! We are linking to this particularly great content on our website.
Keep up the great writing.

Domino99July 25th, 2019 at 6:14 pm

I am regular reader, how are you everybody?
This piece of writing posted at this website is in fact pleasant.

arcade FireJuly 25th, 2019 at 6:24 pm

Its such as you learn my mind! You appear to grasp a lot approximately this,
like you wrote the book in it or something. I feel that you could do with some
percent to power the message home a little bit, but instead
of that, this is excellent blog. A fantastic read. I will definitely be
back.

situs poker bri online 24 jamJuly 25th, 2019 at 6:28 pm

Thanks for sharing your thoughts about exciting game.
Regards

og youtubeJuly 25th, 2019 at 6:36 pm

My developer is trying to persuade me to move to .net from PHP.

I have always disliked the idea because of the costs.
But he’s tryiong none the less. I’ve been using Movable-type on a number of websites for about a year and am concerned about switching
to another platform. I have heard very good things about blogengine.net.
Is there a way I can import all my wordpress
content into it? Any help would be really appreciated!

harga rumah kayuJuly 25th, 2019 at 6:40 pm

Hey very interesting blog!

CASINO BONUSARJuly 25th, 2019 at 7:09 pm

I am now not certain the place you are getting your info,
however great topic. I must spend some time finding out more or understanding more.
Thanks for magnificent info I used to be searching for this info for my mission.

Bandar BolaJuly 25th, 2019 at 7:15 pm

Hi, this weekend is fastidious for me, for the
reason that this moment i am reading this impressive informative paragraph here at my house.

tiktokJuly 25th, 2019 at 7:32 pm

Hey! I could have sworn I’ve been to this blog before but after browsing through some of the post I realized it’s new to
me. Anyways, I’m definitely happy I found it and I’ll be bookmarking and checking back frequently!

tin mớiJuly 25th, 2019 at 7:43 pm

I’m gone to inform my little brother, that he should also
pay a quick visit this web site on regular basis to get updated from newest
news.

tin mớiJuly 25th, 2019 at 7:43 pm

I’m gone to inform my little brother, that he should also
pay a quick visit this web site on regular basis to get updated from newest
news.

HanneloreJuly 25th, 2019 at 7:45 pm

You really make it appear so easy along with your presentation but
I find this topic to be really one thing that I think I might never understand.
It seems too complicated and extremely huge for me. I’m having a
look ahead in your next publish, I’ll attempt to get the
grasp of it!

bandar cemeJuly 25th, 2019 at 7:49 pm

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

Great info. Lucky me I recently found your website by accident (stumbleupon).
I have book-marked it for later!

PokervJuly 25th, 2019 at 8:04 pm

I’m truly enjoying the design and layout of your blog.
It’s a very easy on the eyes which makes it much more enjoyable for me to come here and visit more often. Did you hire out a designer
to create your theme? Great work!

Domino QQJuly 25th, 2019 at 8:05 pm

Hello friends, its impressive piece of writing on the
topic of tutoringand completely defined, keep it up all the time.

facial cleanerJuly 25th, 2019 at 8:05 pm

What’s up friends, how is all, and what you wish for to
say about this piece of writing, in my view its really awesome in favor of me.

เดิมพันสล็อตJuly 25th, 2019 at 8:08 pm

Its like you learn my thoughts! You appear to know so much
approximately this, such as you wrote the ebook in it or something.
I think that you can do with some p.c. to pressure the message home a little bit,
however instead of that, that is fantastic blog. A fantastic read.
I’ll definitely be back.

kartu pokerJuly 25th, 2019 at 8:28 pm

I used to be suggested this web site by way of my cousin. I am not
certain whether or not this submit is written by means of him as no
one else know such unique about my difficulty. You’re wonderful!
Thank you!

King ChordsJuly 25th, 2019 at 8:34 pm

I got this web page from my buddy who informed me about
this web site and now this time I am visiting this site and reading very informative content here.

judi poker online TerpercayaJuly 25th, 2019 at 8:37 pm

Hello! I simply would like to offer you a big thumbs up for the excellent information you’ve got right here on this post.
I will be coming back to your blog for more soon.

I was wondering if you ever thought of changing the structure of your site?

Its very well written; I love what youve got to say.
But maybe you could a little more in the way of content so people could
connect with it better. Youve got an awful lot of text for only
having 1 or two pictures. Maybe you could space it out better?

family law montgomery county paJuly 25th, 2019 at 8:41 pm

I just like the valuable info you supply to your articles.

I will bookmark your weblog and check once more here regularly.

I’m rather sure I will be informed many new
stuff right here! Good luck for the next!

Situs Pokerv TerbaikJuly 25th, 2019 at 8:49 pm

I’m not sure where you’re getting your info, but
great topic. I needs to spend some time learning more or
understanding more. Thanks for excellent info I was looking for this information for my mission.

chelating agentJuly 25th, 2019 at 9:03 pm

This is my first time pay a quick visit at here and i am really impressed to read everthing at one place.

yahoo.co.ukJuly 25th, 2019 at 9:04 pm

It is in reality a nice and useful piece of info. I’m glad that you shared this helpful info with us.

Please keep us up to date like this. Thanks for sharing.