A Blog on Wndows Server and Microsoft Exchange by Thomas Pätzold

Archive for the ‘Commandline’ Category

Advanced Mailboxstatistics

leave a comment »

As an administrator of a large Exchange System you have configured a mailboxquota for each mailbox. But the interesting question is what is the ratio between the configured quota and the used mailboxstore. In order to get this value you have to create your own Windows Powershell function.

The following function enumerates all data and stores it in a custom object. So you are able to work with this object.

function get-mailboxquotausage {
	This function enumerates the configured quota and used mailboxstore for each mailaddress
	of a maildomain. The user is able to specify the maildomain as a parameter to this function.
	This script enumerates all mailaddresses from one primary maildomain. Displayname, primary SMTP address, prohibit send quota and the used mailboxstore are displayed.
.PARAMETER Maildomain
	This parameter specifies the maildomain this function will work on.
	get-mailboxquotausage -maildomain
	This command will show the displayname, primary SMTP address, prohibit send quota and used mailboxstore for the domaine
	get-mailboxquotausage -maildomain | select displayname, @{label="size (MB)"; expression={"{0,0:N0}" -f $_.size}}, @{label="quota (MB)"; expression={"{0,0:N0}" -f $_.quota}}

	This command will show the displayname, primary SMTP address, prohibit send quota and used mailboxstore for the domaine
	The displayed values will be displayed with a decimal point according to the configured language setting.

param([Parameter(Mandatory=$True, ValueFromPipeline=$True)][string]$maildomain)

begin {

$delete = 0
$result = @()
$users=get-mailbox |? {$_.primarysmtpaddress -like "*$maildomain*"}

foreach ($user in $users) {

	$userstats=Get-MailboxStatistics $user 

	$obj=new-object psobject
	$obj = @{}
	$obj.add("Displayname", $user.displayname)
	$obj.add("Mailaddress", $user.primarysmtpaddress)
	write-host $user.primarysmtpaddress
	if (-not $user.prohibitsendquota.isunlimited) {
		$obj.add("Quota", ($user.prohibitsendquota.value.toMB()))
	} else {
		$obj.add("Quota", "unlimited")
	$obj.add("Size", ($userstats.totalitemsize.value.toMB()))
	$result += new-object -type PSObject -Property $obj



Written by Thomas Pätzold

January 28, 2013 at 9:40 pm

UNIX2DOS using Powershell

leave a comment »

Today I was asked to convert a file from the UNIX fileformat to the DOS fileformat.
As you probably know the format is different in the last character of each line. The DOS format ends with a carriage return (Cr) line feed (Lf) character whereas the UNIX format uses the line feed (Lf) character.
So this is a very simple task using Powershell

get-content <filename> |% {$_.replace("`n", "`r`n")} | out-file -filepath <new filename>

Remember the “%” means foreach-object. So this oneliner runs through the whole file and replaces the Lf with the CrLf character.

If you need a solution to use in a pipeline like the UNIX2DOS Unix command write a function that accepts the input from pipeline. The function looks like

function unix2dos{
param([Parameter(Mandatory=$true, ValueFromPipeline=$True)][String]$line)
begin {}
process {
$result = [string]$line.replace("`n","`r`n")
end {}

Call this function like:

get-content <filename> | unix2dos | out-file -filepath <new filename>

If you put this function in your Powershell profile, you are able to use is anytime anywhere.

Written by Thomas Pätzold

January 21, 2013 at 9:59 pm

The Exchange 2010 Powershell SnapIns

leave a comment »

Applies to Exchange 2010 SP1

If you start the Exchange Management Shell, a Powershell instance is started and the Powershell remoting feature is used in order to establish a connection to the Exchange Server System. There is another option to load the Exchange 2010 specific commands in Powershell. In this case you have to start the Powershell and load the necessary Exchange 2010 management snapins.

The following three Exchange 2010 Snapins are available


In order to load the snapins you are able to use the following command

add-pssnapin -Name Microsoft.Exchange.Management.PowerShell.E2010

If you are writing a Powershell script, it might be useful to check if the plugin is loaded. In order to do this you are able to use the following code snipset.

if (! (Get-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction:SilentlyContinue) )
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction:Stop

What is the difference between the Exchange Management Shell and adding the Exchange snapin to the Powershell session?

  1. The Exchange commands using the Exchange Management Shell are implemented as functions. If you load the “Microsoft.Exchange.Management.PowerShell.E2010” snapin instead of using the Exchange Management Shell the commands are implemented as cmdlets.
  2. Using the Exchange Management Shell you are able to take a more detailed look at how the functions better called the wrapper functions are implemented by using the command.
    get-command get-mailbox -detailed | select-object scriptblock

  3. There is one more important diffrence. If you want to use the systemvariable $? it is better to use the loaded snapin instead of the Exchange Management Shell because these commands don´t work properly in the Exchange Managemnt Shell. A second problem is the use of the common parameters because they are not implemented for all functions in Exchange Management Shell.

If you want to start an Exchange Shell Session because you want to execute your command on a special Exchange Server then this is possible but keep the limitations of the Exchange Management Shell in mind.

Written by Thomas Pätzold

September 14, 2011 at 9:10 pm

%d bloggers like this: