PowerShell 101 for Messaging Administrator and IT Pros (Part 4)

If you would like to read the other parts in this article series please go to:

Managing output through pipe, format and select cmdlets

PowerShell allows us to manipulate data with just a couple of extra commands and that is the beauty of it! Knowing a couple of useful cmdlets to transform and manipulate data to start with will mean it’s just a matter of time before you learn new cmdlets and adapt them to our needs.

Our first component is the Format cmdlets. PowerShell provides different views of the same data through Format cmdlet combined with some of its variances such as: list, custom, table or wide. Sometimes when we run a simple cmdlet such as get-mailbox <mailbox-name> we don’t realize how much information we have available just by running the cmdlet, the output is simply just 4 fields (Figure 01). However, using pipe and FL (short for Format-List) we have all attributes related to that mailbox (Figure 02), pretty powerful, huh?


Figure 01


Figure 02

The usage of pipe is a key piece to master PowerShell and it allows us to use the output of a cmdlet as input for the following cmdlet and we can keep combining cmdlets as long as we want (and the data being managed allows us to do that as well). In order to demonstrate what we can do using pipe we should first take a look at the Select-Object cmdlet.

As we have just seen in the previous figure, a simple cmdlet can contain several objects however, we see just a small subset of the information when we run a simple cmdlet.Iif we want to check out all attributes and values of a specific cmdlet we should use “| FT”. This is an important concept as what you see as output of a cmdlet is not the entire picture, the information is always there but we are not seeing it because it is not being displayed. Keep that in mind during your script development.

Let’s use Select which is the alias for Select-Object cmdlet to format the output with only the information required by the administrator. Let’s say that instead of the standard output that we have with the cmdlet we would like to have just the Name, Alias, Database and the ServerName. This can be done easily using the following syntax:

Get-Mailbox | Select Name,Alias,Database,ServerName

So, what happened in the cmdlet above? Basically the first cmdlet (Get-Mailbox) brought you all mailboxes of your organization (as matter of fact the first 1000 entries, if you have more than that you need to use –ResultSize:<number>) however, we have a pipe which means this output will be used by the following cmdlet which is Select Name, Alias, Database, ServerName. The output that now shows up on our screen is a list of all mailboxes but using only those parameters (Figure 03).


Figure 03

Awesome, now we want to add that output to a text file for future review, we can easily combine the current sequence with the cmdlet Export-CSV <File-Name>. Here is the syntax:

Get-Mailbox | Select Name,Alias,Database,ServerName | Export-Csv C:\report.txt

You may have noticed that running the cmdlet above doesn’t bring an output, why? Because the output that was being displayed in the console before was used by the Export-CSV <file-Name> cmdlet which sends everything to the csv file and nothing to the console. We can deal with that by adding a new “| notepad <file>” as shown in Figure 04.


Figure 04

So, now we are using 4 cmdlets and the output of a cmdlet is being used by another one, cool isn’t it? Basically, the first one provided the list of all mailboxes; the second one defined the boundaries for 4 fields that we need the information for; the third cmdlet moved the data to a CSV file; and the last one brought the information using notepad.

Remember, your imagination is the limit, let’s say that after generating the CSV you want to send that by e-mail. Not a big deal, just add

Send-MailMessage -to “<To-SMTP-Address>” -from “<From-SMTP-Address> ” -subject “Mailbox Report” -Attachments “<File-Generated-Previously>” -SmtpServer <Server>

Note:
Bear in mind, that you should have a Receive Connector configured as Anonymous in order to make the cmdlet above work, otherwise you need to pass your credentials with the cmdlet.

Great, we are able to concatenate a lot of cmdlets together and select what we want in our output however; this is not enough for a messaging administrator. We must be able to filter and by just doing that it opens up a universe of actions and ideas to build scripts.

In order to filter the information we use the Where-Object { <attribute> <comparison-operator><value>} cmdlets.

A good example is filtering all users from a specific Organization Unit and instead of passing the full string we can use like operator and use wildcard for the string that we are filtering, the syntax is listed here:

Get-Mailbox | Where-Object { $_.OrganizationalUnit -like ‘*PortoAlegre’ }

But this is too simple, we may need a list of all users of a specific Organization Unit and that also have their mailboxes hosted in a specific database, using the following syntax:

Get-Mailbox | Where-Object { ($_.Database -eq ‘<DB-Name>’) -and ($_.OrganizationalUnit -like ‘*PortoAlegre’) }

Bear in mind, that the boundary of Where-Object will be the {} and if we are going to use more than one expression then we need to encapsulate each expression with () and use a –and comparison operator between them (in our previous example was –and which means both clauses must be true).

Nicely done, if you haven’t slept at this point it seems that you are really into it, which is great! So, time to put all things together. Now we have the power and we can filter, select the fields that we want to, and use cmdlets with pipes to provide better results.

Let’s continue the sequence that we have done so far, basically, we want to build a line to list all mailboxes that are located in a specific database and they must also be in a specific OU, and we want to have only their Name, Alias, Database and ServerName, and all of that must be in a csv file. So, using 5 cmdlets and one line we can get the information, as shown in Figure 05.

Get-Mailbox | Where-Object { ($_.Database -eq ‘POA-DB01’) -and ($_.OrganizationalUnit -like ‘*PortoAlegre’) } | Select Name,Alias,Database,ServerName | Export-Csv C:\Report-XXX.csv | notepad C:\Report-XXX.csv


Figure 05

Conclusion

In this article we went over a couple of useful features that are required to understand PowerShell.

If you would like to read the other parts in this article series please go to:

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top