Register. Track Progress. Earn Credits.
Learning has never been so easy!
Sign Up

I thought it would be helpful to start a series of how to do some basic things in Powershell.

If you are interested in learning Powershell feel free to follow me because I'm going to try and write these short powershell tip posts each week.

Scenario:

Boss walks over and asks "Can you email me exactly who is a member of _____ AD group?"

Their are a number of ways to approach this. You could open up AD Users and Computers, find the group, and then take screen shots. Or if you still had the old "local" command back in the Windows 2000 days use it to query AD. But you know a better way... Powershell!

Open your Powershell window or Powershell ISE and type the following cmdlet with your group name.

Powershell
PS C:\> Get-ADGroupMember "Name of Group"
Be sure to replace "Name of Group" with the group you want to get a list of memberships from. For example if you wanted to see who is a member of your Domain Admins group then you would type in:
Powershell
PS C:\> Get-ADGroupMember "Domain Admins"

The output is going to be a lot of information. You may not want all this information, and you know your boss doesn't! He/she just wants to know what accounts are listed so lets format this output a little better for them. You can do this by using the Format-Table cmdlet. This allows you to grab just the info you needed. So for example lets say you wanted this output in a single column of user names just type the following:

Powershell
PS C:\> Get-ADGroupMember "Domain Admins" | Format-Table samaccountname

If you noticed we used the "|" character which basically tells powershell to take the output of the first cmdlet (Get-ADGroupMember) and give it to the following cmdlet (Format-Table). Now you have the user accounts. But what if you wanted another column such as the name of the account? Easy to do! Try this:

Powershell
PS C:\> Get-ADGroupMember "Domain Admins" | Format-Table name, samaccountname

Now you have two columns AND its formatted real nicely!


Difficulty Level: Beginner
Tagged in:
 

40 Comments

Mini magick20170517 15105 gp0236 big
Pure Capsaicin
Rob Dunn

Awesome, thanks James :)

Mini magick20140701 23064 in9eln big
Habanero
Holo

Sweet stuff, and helped me understand some stuff I didn't understand before.

Mini magick20140428 27959 1b338y1 big
Thai Pepper
MattSpeller

+1

More please.

Mini magick20171226 48008 m3ki26 big
Mace
Neally

Damn, nice, thanks!

Mini magick20151026 20466 xi04ez big
Datil
James for Microsoft

Glad you guys are finding this useful!

Mini magick.23126.41847 big
Cayenne
KevinRak

I've already learned a decent amount of PowerShell, but this is still very useful info to have. Plus I've linked it to the rest of my team so that they'll stop looking at me like I'm talking French when I start explaining a PowerShell script.

Mini magick.21491.39708 big
Thai Pepper
BloodHoundGang

Thank you

Mini magick20151026 26422 1qneb9g big
Chipotle
Scott1133

Thanks James. I love the utility of powershell and am always happy to pick up more knowledge

Mini magick20151030 7085 euzq8y big
Thai Pepper
wowitsdave

Hey, cool. You're a day late. Needed this one yesterday :).

I still <3 PowerShell though.

Mini magick20150908 25801 58vb2b big
Cayenne
Travis Finch

Keep this up thank you +1

Just printed for my desk for quick reference.

Mini magick.22062.76438 big
Chipotle
sysadmnate

+1  Thanks for this.

Mini magick20151026 1898 hles25 big
Pure Capsaicin
Martin9700

Quick moderator note:  You don't need to do a +1 post, just spice up the particular post or the entire thread.  I'm supposed to delete these posts (and I will going forward) but I'll let these stand cause I'm a nice guy ;)  Not to mention if I did I think only 2 or 3 posts would survive!!

Thanks everyone!

Mini magick20151027 30532 wxoqnj big
Cayenne
squeak
Was just about to complain this doesn't work on my server, but it does as long as you run the right powershell window, in this case the Active Directory Module for Powershell

Why wouldn't this just work in the standard powershell commandline, or to put it in other words, why do we have seperate powershell windows for various server elements like Exchange too?

Thanks!


Mini magick20140926 30977 1nptuqf big
Poblano
James.Fleck

squeak wrote:

Was just about to complain this doesn't work on my server, but it does as long as you run the right powershell window, in this case the Active Directory Module for Powershell

Why wouldn't this just work in the standard powershell commandline, or to put it in other words, why do we have seperate powershell windows for various server elements like Exchange too?

Thanks!

Import the active directory module (import-Module ActiveDirectory) into any powershell window and it will work.

Mini magick20140219 19708 t2uyku big
Cayenne
Yotta

very nice & simple

Mini magick20151027 30532 wxoqnj big
Cayenne
squeak

James.Fleck wrote:

squeak wrote:

Was just about to complain this doesn't work on my server, but it does as long as you run the right powershell window, in this case the Active Directory Module for Powershell

Why wouldn't this just work in the standard powershell commandline, or to put it in other words, why do we have seperate powershell windows for various server elements like Exchange too?

Thanks!

Import the active directory module (import-Module ActiveDirectory) into any powershell window and it will work.

Thanks James :)
Mini magick20151030 27261 e5oaxp big
Thai Pepper
SteveTheITDude

Sweet, keep them coming!

Avatar user
Anaheim
DLStoehner

I don't even know how to open a Power Shell window. Can you start with the very most basic stuff for a NOOB like me please?

Thanks, David

Mini magick20141112 20096 1lrsxmv big
Sonora
MichaelTS

Just last week I used powershell for the first time - to update a Windows 8.1 installation in OOBE mode (since Win 8.1 doesn't allow updates in OOBE mode -.-) - and I liked it. Now I updated my study list from Visualization, Win Server 2012 to Visualization, Win Server 2012 and Powershell.
Your Powershell Tips Series fits perfectly. Thanks a lot!

Mini magick20141112 20096 1lrsxmv big
Sonora
MichaelTS

James.Fleck wrote:

Import the active directory module (import-Module ActiveDirectory) into any powershell window and it will work.

Since I am totally new to PS... on my test server, I was able to run the script without a problem. Could it be that the module is imported by default? I once had to import a module which I downloaded seperatly, but the ActiveDirectory module was there from the beginning, no?
Mini magick20151026 1898 hles25 big
Pure Capsaicin
Martin9700

With PowerShell 3.0 if the module is already installed it will automatically import it (Import-Module ActiveDirectory, for example).  This does not work for snap ins.

Mini magick20141112 20096 1lrsxmv big
Sonora
MichaelTS

Cool, thx for the fast reply!

Mini magick20151026 21170 x6tunl big
Mace
Chamele0n

Great tip James. keep em' coming.

Mini magick20140918 28067 103ns6 big
Poblano
Kacy Meadows

This was very helpful. Thanks for the tips!