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)

escortJuly 27th, 2019 at 12:21 pm

What’s up Dear, are you really visiting this web site on a
regular basis, iff so after that you will definitely obtain nice know-how.

hereJuly 27th, 2019 at 12:21 pm

Thanks a lot for sharing this with all people you actually realize what you’re talking about!

Bookmarked. Please also seek advice from my web site =).
We can have a hyperlink alternate arrangement between us

situs bola onlineJuly 27th, 2019 at 12:25 pm

Awesome blog you have here but I was wanting to know if you knew of any discussion boards that
cover the same topics talked about here? I’d really
love to be a part of community where I can get feed-back from other
experienced individuals that share the same interest.
If you have any recommendations, please let me know.
Cheers!

mobile legends hack april 2019July 27th, 2019 at 12:26 pm

It’s impressive that you are getting ideas from this piece of writing as well as from our
discussion made at this place.

free stuffJuly 27th, 2019 at 12:36 pm

Woah! I’m really enjoying the template/theme
of this website. It’s simple, yet effective. A lot of
times it’s hard to get that “perfect balance” between user friendliness and visual appearance.
I must say you have done a amazing job with this.
Additionally, the blog loads very fast for me on Opera.
Outsxtanding Blog!

Kartu PokerJuly 27th, 2019 at 12:37 pm

Hi, after reading this awesome article i am too happy to share my knowledge here with colleagues.

Head UnitJuly 27th, 2019 at 12:40 pm

If some one wishes expert view concerning blogging and site-building then i
recommend him/her to visit this web site, Keep up the pleasant job.

kartu pokerJuly 27th, 2019 at 12:41 pm

Undeniably consider that which you stated. Your favorite
reason seemed to be at the internet the easiest thing to take note of.
I say to you, I definitely get annoyed whilst other people
consider issues that they plainly do not realize about.
You controlled to hit the nail upon the highest and also
outlined out the whole thing with no need side effect , other people can take a signal.
Will likely be back to get more. Thanks

PBTC chemicalsJuly 27th, 2019 at 12:43 pm

May I just say what a comfort to find a person that
truly knows what they’re discussing online. You certainly understand
how to bring a problem to light and make it
important. More people ought to read this and understand this side of your story.
I was surprised you’re not more popular because you most certainly possess the gift.

facial skin scrubberJuly 27th, 2019 at 12:48 pm

Great blog! Is your theme custom made or did you download it
from somewhere? A theme like yours with a few simple
tweeks would really make my blog shine. Please let me know where you got your
theme. Appreciate it

cheap viagra pills free shippingJuly 27th, 2019 at 12:49 pm

availability of sildenafil in islamabad viagra online canada
pharmacy using sildenafil reviews

vendor advocacyJuly 27th, 2019 at 12:50 pm

My brother suggested I may like this blog. He was totally right.
This publish actually made my day. You can not believe simply how so
much time I had spent for this information! Thanks!

buy viagraJuly 27th, 2019 at 1:00 pm

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

cialis onlineJuly 27th, 2019 at 1:01 pm

couple levitra ou tadalafil http://cialisps.com tadalafil prise journaliere

agen BolaJuly 27th, 2019 at 1:06 pm

Why users still make use of to read news papers when in this technological world all is accessible on net?

pornJuly 27th, 2019 at 1:09 pm

Very good blog! Do you have any tips and hints for aspiring writers?
I’m hoping to start my own blog soon but I’m
a little lost on everything. Would you suggest starting with a free platform like WordPress or go for a paid option? There are so many choices out there
that I’m totally overwhelmed .. Any suggestions?
Bless you!

Streaming JavJuly 27th, 2019 at 1:09 pm

Thank you for every other informative web site. Where
else may just I am getting that type of information written in such an ideal manner?

I’ve a mission that I’m simply now operating on, and I’ve been at the look
out for such info.

agen bola indonesiaJuly 27th, 2019 at 1:12 pm

Hey there! Someone in my Myspace group shared this website with us so I came to check it out.

I’m definitely loving the information. I’m book-marking and
will be tweeting this to my followers! Wonderful blog and excellent design.

CarmellaJuly 27th, 2019 at 1:14 pm

Hmm is anyone else having problems with the pictures on this blog loading?
I’m trying to determine if its a problem on my end or if it’s the blog.
Any feedback would be greatly appreciated.

viagra onlineJuly 27th, 2019 at 1:15 pm

[url=http://vagragenericaar.org/]cheap viagra canada[/url]

Free Watch JavJuly 27th, 2019 at 1:19 pm

I must thank you for the efforts you’ve put in penning this website.
I’m hoping to check out the same high-grade blog posts by you in the future as well.
In fact, your creative writing abilities has inspired me to get my own, personal website now 😉

parisJuly 27th, 2019 at 1:23 pm

Fantastic goods from you, man. I have understand your stuff previous to and
you’re just too fantastic. I actually like what you have acquired here,
certainly like what you are saying and the way in which you say it.
You make it entertaining and you still take care of to keep it sensible.
I can’t wait to read far more from you. This is actually a terrific web site.

Voip GuideJuly 27th, 2019 at 1:27 pm

Utilizing this choice (otherwise called the “unending” or “on-premises” form of Office), you can introduce the applications on a
solitary PC and you’ll get security refreshes, yet you won’t most likely move up
to a subsequent adaptation https://voip-guide.org/

카지노사이트July 27th, 2019 at 1:28 pm

Howdy excellent website! Does running a blog like this require a lot of work?

I’ve no expertise in computer programming but I had been hoping to start my own blog soon. Anyhow,
should you have any recommendations or tips for
new blog owners please share. I understand this is off subject nevertheless I just
wanted to ask. Thanks!

Agen DominoJuly 27th, 2019 at 1:30 pm

Hello i am kavin, its my first occasion to commenting anywhere,
when i read this paragraph i thought i could also create comment due
to this sensible article.

Free Watch JavJuly 27th, 2019 at 1:34 pm

Hello everyone, it’s my first go to see at this web site, and piece of writing is actually fruitful in favor of
me, keep up posting these types of content.

cheap viagra fast shippingJuly 27th, 2019 at 1:38 pm

australia sildenafil over the counter viagra for sale cheap how can i tell if sildenafil is working

온라인카지노July 27th, 2019 at 1:38 pm

WOW just what I was looking for. Came here by searching for 바카라사이트

luxury automobilesJuly 27th, 2019 at 1:42 pm

Hey outstanding blog! Does running a blog like this require a large amount of work?
I have absolutely no expertise in computer programming however I was
hoping to start my own blog in the near future. Anyhow, should you
have any ideas or tips for new blog owners please share.
I understand this is off subject but I simply had to ask.
Thank you!

Law ActuallyJuly 27th, 2019 at 1:54 pm

I do not even know the way I stopped up here, but I thought this put
up was once great. I don’t realize who you might
be however definitely you’re going to a famous blogger should you aren’t already.
Cheers!

upgrading or replacingJuly 27th, 2019 at 1:56 pm

If you are going for best contents like I do, simply visit this web page every day as it
provides quality contents, thanks

agen bola indonesiaJuly 27th, 2019 at 1:57 pm

I do not know if it’s just me or if perhaps everybody else encountering problems with your site.
It seems like some of the text in your content are running off the screen. Can someone else please comment and let me know if this is happening
to them as well? This might be a issue with my internet
browser because I’ve had this happen before. Thanks

vendor advocacyJuly 27th, 2019 at 2:03 pm

This is really fascinating, You’re a very skilled
blogger. I have joined your rss feed and look ahead to seeking more of your magnificent post.
Additionally, I have shared your web site in my social networks

바카라사이트July 27th, 2019 at 2:07 pm

Howdy! This blog post could not be written any better!
Looking at this post reminds me of my previous roommate!
He always kept talking about this. I am going to send this post to him.
Fairly certain he will have a very good read. Many thanks for sharing!

the factJuly 27th, 2019 at 2:27 pm

It’s amazing in support of me to have a site, which is good in favor of my experience.
thanks admin

THPS biocideJuly 27th, 2019 at 2:37 pm

Amazing! This blog looks exactly like my old one!
It’s on a completely different subject but it has pretty much the same
layout and design. Great choice of colors!

water treatment chemicals industryJuly 27th, 2019 at 2:44 pm

Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your further write ups thanks once again.

free downloadJuly 27th, 2019 at 2:45 pm

Thank you for every other wonderful post. Where else may anyone get that type of information in such a perfect means of writing?

I have a presentation subsequent week, and I am at the search for such
info.

viagraJuly 27th, 2019 at 2:55 pm

We stumbled over here different web page and thought I may as well check
things out. I like what I see so i am just following you.
Look forward to going over your web page again.

cheap Private ProxiesJuly 27th, 2019 at 2:55 pm

Since the admin of this web page is working, no
uncertainty very soon it will be renowned, due to its feature contents.

the ideal soundJuly 27th, 2019 at 2:57 pm

The other day, while I was at work, my cousin stole my iPad and tested to see if it
can survive a 40 foot drop, just so she can be a youtube
sensation. My apple ipad is now destroyed and she has 83 views.
I know this is totally off topic but I had to share it with someone!

duanapeters.netJuly 27th, 2019 at 2:58 pm

Hello! I know this is kind of off topic but I was wondering which blog platform are you using for
this site? I’m getting tired of WordPress because I’ve had issues 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.

Rochelle BosistoJuly 27th, 2019 at 3:07 pm

Hello, I enjoy reading through your article. I like to write a little comment to support you.

chelating agentJuly 27th, 2019 at 3:15 pm

Sweet blog! I found it while surfing around on Yahoo News.
Do you have any tips on how to get listed in Yahoo News?
I’ve been trying for a while but I never seem to get there!
Cheers

PutlockersJuly 27th, 2019 at 3:16 pm

You really make it appear really easy along with your presentation but I find this topic to be really something which I think I might by
no means understand. It sort of feels too complex and very vast for me.
I’m taking a look ahead to your next submit, I’ll try to get the hang
of it!

www.see-game.huJuly 27th, 2019 at 3:17 pm

Amazing! Its genuinely amazing post, I have got much clear idea regarding from
this piece of writing.

Http://Kimmburu.Com/July 27th, 2019 at 3:21 pm

This is a topic that is near to my heart… Many thanks! Where are your contact
details though?

buy fast proxyJuly 27th, 2019 at 3:40 pm

Hello, Neat post. There is an issue with your web site in web explorer,
might check this? IE nonetheless is the marketplace chief and a big section of other people
will pass over your woderful writing due to
this problem.