Getting What You Want

The GET family of commands is a staple in PowerShell and the Microsoft Online ecosystem. One of the first hurdles you’ll find yourself looking to overcome is taming the wild beast that is a wide cast GET command. For example, if I was looking for a list of all users with the first name of John in a tenant of <50 users then simply running the following command and looking it over with my bare-eyes (is that a thing?) would be a viable option. (If you’re playing along @ home you’ll want to connect PowerShell to your tenant first.)

Get-MSOLUser

Your output would look something like this, but presumably with more names.

Finding the aforementioned John in a list of 50 names would not be difficult. Suppose you’re working in a tenant of 1000 users though, manually going over a list of 1000 users looking for John is such a daunting task that PowerShell even tries to save you from yourself. Here is our same command run against a much larger tenant.

If you’re stubborn you can run the following command and you’ll get what you were originally looking for but it’s still the equivalent to using a hammer on a stubborn screw.

Get-MsolUser –All

Filtering will allow you to turn bird shot into buckshot (if this analogy went over your head ask a redneck). We’ll take a standard GET command and pipe it into a filter. If you’re looking for more information on piping and PowerShell this is the best resource on the internet.

Practicing Pipelines

I’ll jump forward and show you a command that will get all users named John and then I’ll dive into a bit more detail about what the command does.

Get-MsolUser -all | Where-Object {$_.FirstName -contains "John"}

The first thing this command does is retrieve all users in your tenant. Next we insert our pipeline and then include the condition of where the object (a MSOL User in this case) has a FirstName that contains John.

The Questions

Likely your first question will be how this very handy John finder can be modified for your practical application. Starting with the $_.FirstName component, you can swap the FirstName attribute out for other attributes of the object. In order to determine what other attributes you can filter by you’ll want to see all attributes associated with the object using the following command.

Get-MsolUser –UserPrincipalName nate@get-msol.com | FL

Substitute the bolded username for any username in your tenant. Your output will look something like this…

Any attribute name outlined in the blue box can be used to limit your result set. In this second example we’ll use the IsLicensed attribute to find all licensed users. We’ll also swap from the –contains qualifier to the –eq modifier which is short for equals. If you’re looking for a complete list of qualifiers I recommend the Microsoft TechNet article on the subject of the Where-Object cmdlet.

Get-Msoluser | Where-Object {$_.IsLicensed –eq $False}

This second example showcases a common pitfall which is that attributes which are Boolean values (True or False) will need to use $False or $True instead of the “True” or “False” syntax you would use for string attributes like names or email addresses.

Again! Again! Again!

Only because you insisted, here is a 3rd example. This time we’ll be looking for a certain type of mailbox. Our 3rd example will find us all shared mailboxes in the tenant. To get started we’ll need to find all the attributes associated with mailboxes. The easiest way to gain attribute information is to run a GET command against a sample object (a mailbox in this example) which matches your desired criteria. For a shared mailbox I ran the following on my sample tenant.

Get-Mailbox -Identity testshared@get-msol.com | FL

Now because there are 39,821 (until you count them you don’t know I’m wrong) mailbox attributes I’ll forego the full output. Here is the section which contains the attribute we care about, RecipientTypeDetails.

Our search scope is all mailboxes, we’re concerned with the RecipientTypeDetails attribute, and we know that the RecipientTypeDetails value of the sample shared mailbox is SharedMailbox. Once it’s all glued together we have the following…

Get-Mailbox | Where-Object {$_.RecipientTypeDetails -eq "SharedMailbox"}

And so you don’t think I’m a liar, here is the output... (yes I only have 1 shared mailbox, don’t judge)

The Wrap Up

Hopefully between the 3 examples you’ve begun to get a sense of how the syntax works. You’ll see these type of commands a lot as you work around the Microsoft Online ecosystem so its always a good idea to know what they do and how to modify them to do your bidding.


As always please feel free to email me using the contact form in the sidebar or leave a comment below if you have any questions. I’m happy to help overcome any challenges you’re having or to take your suggestions on future topics you’d like to see me cover.

#pipeline #powershell #ps101

Featured Posts
Recent Posts
Search By Tags
No tags yet.
Connect