The GSX Blog
PowerShell was publically released to the public in 2006 and since then is experiencing a growing interest among the Microsoft World Community IT Pros. The main reason for this success is that it empowers administrators due to its ease of use and powerful scripting capabilities.
It is the successor of the command-line utility and tries to compete with all the shell scripting that the Unix guys benefit from. In this blog post you will find tips related to all basics required to start learning PowerShell and ease your day-to-day life as an administrator.
Basics
The first thing you need to know is that PowerShell commands (called commandlets, and usually referred as “cmdlets”) are always named in the same way of a verb and a noun:
Verb-noun
Therefore it is usually quick and easy to remember commands, especially the ones you will be using on a regular basis. The absolutely first cmdlet you have to know is how to get help and you might have already guessed its name:
Get-Help
Thanks to aliases, Unix guys can keep their old habits and use “man” if they prefer š
It starts to get very easy finding the most basics cmdlets to get system info like processes and windows services:
Get-Service
Get-Process
And PowerShell is (almost) the same for every application that leverages it, therefore you can start the Exchange Management Shell and enjoy the same fun:
Get-Mailbox
Get-MailboxServer
Get-MailboxStatistics
In order to extend the scripting capabilities, cmdlets can be combined with one another using the magical pipe character “|”. When using pipelining in PowerShell every object that is returned from the first cmdlet is passed on to the right side of the pipe. It is important to notice that in PowerShell what is transferred through the pipe are objects and not characters:
Get-Service “GSX*” | Stop-Service -WhatIf
Here what would happen is that every Windows Service that starts with “GSX” will be transferred to the “Stop-Service” cmdlet that will tell what it would do IF we were to run it, as the parameter “WhatIf” was used. Very useful parameter to check and anticipate what will happen before it does. Once you want to execute the cmdlets just remove that parameter.
Formatting and exporting results
In order to format the result you can use the well named “Format-Table” or “Format-List” cmdlets, and choose which properties to return:
Get-Service | Format-Table Name, Status, CanStop -AutoSize
For exporting your results it might be more convenient to convert to a CSV format and write it into a file on disk:
Get-Service | ConvertTo-Csv | Out-File “C:\temp\Services.csv”
You can also refine results using the “Where-Object” cmdlets. In order to use that cmdlet you need to know how to point to the object passed through the pipe, and the object can be referred to as “$_” as in this example, if we want only the “stopped” services:
Get-Service | Where {$_.Status -eq “Stopped”}
If you need more operators you can click here.
You get the idea, it is very easy to do anything you want now.
Exchange administration
So now let’s take a closer look at what you can achieve with these simple cmdlets for your Exchange organization.
Top 10 Mailboxes with the most items:
Get-Mailbox -ResultSize unlimited| Get-MailboxStatistics | Sort-Object “ItemCount” -Descending | Select-Object -First 10 | Format-Table DisplayName, ItemCount, TotalItemSize
Last 20 Entries in the event log that were Errors related to Exchange:
Get-EventLog Application | Where { $_.Source -Ilike ā*Exchange*ā -and $_.EntryType -eq āErrorā} | select -last 20
Total number of Mailboxes in my organization:
(Get-Mailbox -Resultsize unlimited).count
Number of Mailboxes grouped per Mailbox Database:
Get-Mailbox | Group-Object -Property:Database | Select-Object name, count
List of all emails received by a specific email address in the last hour:
Get-MessageTrackingLog -Start (Get-Date).AddHours(-1) -EventId DELIVER -Recipients [email protected]
So that was not too complicated to get all this information for your Exchange organization. Therefore I would greatly encourage you to get more familiar with PowerShell cmdlets, leveraging scripts is quickly becoming the new standard for managing servers and collecting data.
Here are some of our most interresting articles about PowerShell:
- Troubleshooting unknown PowerShell error messages
- PowerShell ISE: Tips and tricks
- PowerShell Tip: Easily manage all your “BYOD” devices
For those of you eager to learn more about PowerShell for Exchange here are some great resources I would like to recommend:
PowerShell for Exchange 2010āCheat Sheet
Generate Exchange Environment Reports using PowerShell
Read our White Paper about PowerShell to be unbeatable! |