Link to home
Start Free TrialLog in
Avatar of DarthRater
DarthRater

asked on

Powershell Beginner

I've been using a cmdlet called ActiveRoles Management Shell for Active Directory and I am really able to pull some great AD data with it, but I am new to Powershell and have some questions.

For example, I can run Get-QADUser  -Enabled and it give me a very verbose output. Is there a way to pare this down any/reduce the fields it returns?? Also, when a command is preceded by a dash, what is that command called? And where can I find a list of commands that use that? Searching Google for "what is is called when a dash precedes a command" gets me nowhere. And get-command is not what I am looking for.

Thanks in advance.
Avatar of Dale Harris
Dale Harris
Flag of United States of America image

I love beginners.  One of the best ways to learn is just to play with it.

Here's an article I wrote that is a "step 2" article.  I also link back to the step 1 as well.

It includes commands for the Active Roles Server Add-in.

https://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/A_4327-PowerShell-Where-do-I-start.html

In regards to your question, you can easily pull back whatever you want and put it into an array (list) for use later.

Example:

get-qaduser -enabled | select Name, DN

This will only bring back the Name and DN for display on the screen.

If I were you, I would go a step further and put it into an array.

$Users = get-qaduser -enabled

Now you can see what you can do with it by typing in the following command:

$Users | gm

GM is an alias for get-member, so either will work.  Basically it will show you all the properties/methods.  This list is very extensive, but not as extensive as pulling up your adsiedit.msc and getting the list of attributes available for a user.

It shows you that name is available.  So iterate through your list and show the names of each person:

$Users | foreach-object{$_.name}

You can also substitute foreach-object with the percentage symbol %, so it looks like this:

$Users | %{$_.name}

The $_.property signifies "Whatever you started with, bring it over to the right and use it as an object".

The pipeline is really important to understand early so I recommend the following way to think about it:

You start out with a bunch of users.  We will call this our marathon.  Now, you want to filter out some people in your search.  So you do a search for every user with an expired password.  This would be like asking for every person in the marathon that's a male.

It would look like this:

$Users | ?{$_.passwordisexpired}

Now let's see who's name starts with an "H", so it would be similar to asking for everyone that is under 30.

$Users | ?{$_.passwordisexpired} | ?{$_.name.startswith("H")}

So now we have a list of people that made it to the end outputted to the screen when we run this.  Every user that their password is expired and starts with an H.  Or every person in the marathon that is a male under 30.

If you want to store that to a different list, you can simply add a $RefinedUserList = to the beginning to look like this:

$RefinedUserList = $Users | ?{$_.passwordisexpired} | ?{$_.name.startswith("H")}

You won't get an output, but when you do a "$RefinedUserList.count" it will tell you how many users got into the list.

Then you can see the list by typing in $RefinedUserList

Then you can output that to a text file by typing in $RefinedUserList > Users.txt

This will create a Users.txt file with that list in your directory you're working from.

Try to work from C:\scripts as your main working script directory.

Good luck and happy scripting!

Dale Harris
ASKER CERTIFIED SOLUTION
Avatar of Dale Harris
Dale Harris
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DarthRater
DarthRater

ASKER

0_o....good lord. Best answer ever! Thank you!
Avatar of Qlemo
http:#a37316250 is the reason why you do not get anywhere with get-command or Google search, if just search for the parameter name ;-).
You need to view the cmdlet itself for which parameters it allows. get-help get-wmiobject   for example.