RSS Subscription 167 Posts and 2,643 Comments

A few Exchange PowerShell scriptlets

Figured I would share a few scriptlets I’ve written recently.  Hopefully some of you may find them useful at some point at least.

Scriptlet 1

Find out all users who live on a specific mailbox without having to type the entire mailbox database name:

?View Code POWERSHELL
1
get-mailbox -database $(get-mailboxdatabase | where-object {$_.name -like "*Database1*"}) | Format-Table Name,Database -wrap -autosize

Scriptlet 2

Get statistics on a specific user without knowing the entire name or if you’d want to find out statistics on all users who have the first name John or last name Doe:

?View Code POWERSHELL
1
Get-MailboxStatistics -server serverhere | Where-Object {$_.DisplayName -like "*MailboxUserName*"} | Format-Table DisplayName,ItemCount,TotalItemSize

Scriptlet 3

When you are in an environment with multiple Mailbox Servers, it may be annoying to find what disconnected mailbox lives on which server since the Exchange Management Console will only connect to one server at a time and when connected, it’ll only show disconnected mailboxes for that specific server.  This scriptlet will cycle through all mailbox servers and report back all disconnected mailbox servers and which disconnected mailbox lives on which mailbox server.

?View Code POWERSHELL
1
2
3
4
$mbx = Get-ExchangeServer | Where-Object {$_.IsMailboxserver -eq $true}
foreach ($server in $mbx) {
	Get-Mailboxstatistics -Server $Server | Where-Object { $_.DisconnectDate -ne $null } | Format-Table DisplayName,ItemCount,OriginatingServer -wrap
}

Scriptlet 4

Based on a CSV file, export users contacts that were created after a certain start date to a PST file.  A copy of how the CSV should be formatted is located here.  While the CSV contains multiple columns, the script only utilizes the Account Name column by default.  If you want to modify what columns it uses, you can change the line below that starts with $AdName =.  After the script pulls the name in the Account Name field, it finds the mailbox using the Get-Mailbox command with the name that was specified in the Account Name field.

?View Code POWERSHELL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
$file = "pstusers.csv"
$PSTLocation = "C:\PSTs"
$StartDate = "03/20/2009"
 
############################################
####### Don't modify below this line #######
############################################
$erroractionpreference = "SilentlyContinue"
if (!(Test-Path -path $PSTLocation)) {
	Write-Host "Creating the Directory $PSTLocation" -ForegroundColor Yellow
	New-Item -ItemType "Directory" -Path $PSTlocation
}
if (Test-Path $file) {
	$excel = Import-CSV $file
	foreach ($line in $excel) {
		$AdName = $line."Account Name"
		$Mailbox = Get-Mailbox $AdName
		if ($Mailbox) {
			Write-Host "$AdName is being exported to $PSTLocation"
			$Mailbox | Export-Mailbox -PSTFolderPath $PSTLocation -StartDate $StartDate -ExcludeFolders "\Inbox","\Deleted Items","\Drafts","\Junk E-mail","\Outbox","\Sent Items","\Journal","\Calendar","\Notes","\Tasks"
		}
		else { Write-Warning "$ADName Mailbox Does not Exist" }
	}
}
else {
	Write-Warning "The file $file does not exist"
}

Scriptlet 5

If you are familiar with Quest Notes Migrator for Exchange, you may have had the chance to migrate data to a PST file.  What you will see, is that the PST gets migrated with the Display Name of the user.  For example, we if migrated the Notes mailbox of Elan Shudnow to a PST, the PST file name would be Elan Shudnow.PST. A lot of people won’t want to migrate the PST directly from Quest NME to a user’s home share because that user’s home share may be located in a remote site and opening a PST file across a network isn’t really a good idea; especially over long distances.

So I wrote a script so you can migrate all the PST files locally and then it’ll check the Display Name portion of the PST file and use that display name to find a matching user in AD and then find their home directory and then copy that PST file to that home share.

?View Code POWERSHELL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Set Location to where the PST files are started
Set-Location "E:\"
 
$pstData = Get-ChildItem -recurse | Where-Object {$_.Name -like "*.pst"}
 
function Get-HomeDirectory ($displayName) {
	$ads = New-Object System.DirectoryServices.DirectorySearcher([ADSI]'')
	$ads.filter = "(&(objectClass=Person)(displayName=$displayName))"
	$s = $ads.FindOne()
	return $s.GetDirectoryEntry().homeDirectory
}
 
foreach ($pstUser in $pstData) {
	$PSTName = $pstUser.Name
	$PSTDisplayName = $pstUser.Name.split(".")[0]
	$PSTDisplayName = $PSTDisplayName.split("-")[0]
	$Path = $PSTUser.DirectoryName + "\" + $PSTUser.Name
	$Directory = Get-HomeDirectory $PSTDisplayName
	Copy-Item $Path $Directory
}
Share

11 Responses to “A few Exchange PowerShell scriptlets”

  1. [...] A few Exchange PowerShell scriptlets Posted on May 15, 2009 by johnacook http://www.shudnow.net/2009/05/14/a-few-exchange-powershell-scriptlets/ [...]

  2. on 16 May 2009 at 5:22 pmShay Levy

    Hi Elan,

    Scriptlet 1: Get-MailboxDatabase Identity parameter accept wildcard characters so you could skip Where-Object and do:

    Get-MailboxDatabase -Identity *Database1* | Format-Table Name,Database -wrap -autosize

  3. on 17 May 2009 at 8:07 amElan Shudnow

    Can’t believe I didn’t know that! *slaps self* Thanks Shay.

  4. on 11 Dec 2009 at 9:14 amshany

    Hi Elan

    Could you please add one script or command line ,where we can use both Get-mailbox and Get-MailboxStatistics?

    I have one example ,but its trhrowing error

    "MailboxDisplayName,SAMAccount,EmployeeID,Department,ServerName/StoreName,Size,TotalItems,LastLoggedOnUserAccount,LastLogoffTime,LastLogonTime"| out-file GMS.csv;Get-Mailbox -resultsize unlimited -server st-excl21 | Get-MailboxStatistics -server st-excl21 | ForEach{$a = $_.DisplayName;$b=$_.Alias;$c=$_.CustomAttribute12;$d=$_.CustomAttribute4;$e=$_.database;$f=$_.TotalItemSize.Value.ToMB();$g=$_.itemcount;$h=$_.LastLoggedOnUserAccount;$i=$_.LastLogoffTime;$j=$_.LastLogonTime;"$a,$b,$c,$d,$e,$f,$g,$h,$i,$j"} | out-file GMS.csv -Append

    Please help

  5. on 30 Mar 2010 at 1:44 pmGary

    Do you know how to script the sharing of the calendar for a shared mailbox

  6. on 30 Mar 2010 at 8:04 pmElan Shudnow

    Use exfolders for Exchange 2010 and PFDAVAdmin for Exchange 2003/2007.

  7. on 15 Sep 2011 at 4:50 amRrrrr

    for script 3 how can i get it to either:_a. output in alphabetical order_or_b. output to a spreadsheet like excel so i can sort the order alphabetically_the script is great but having them out of order i have to constantly watch the screen and we have hundreds to go through sometimes.

  8. on 21 Sep 2011 at 3:19 pmElan Shudnow

    Change this line:
    Get-Mailboxstatistics -Server $Server | Where-Object { $_.DisconnectDate -ne $null } | Select-Object DisplayName,ItemCount,OriginatingServer | Export-CSV C:\file.csv -notypeinformation

    Basically, the Format-Table is Powershell Formatting only. Select-Object is agnostic and allows you to pipe the results using Export-CSV.

  9. on 09 Feb 2012 at 6:04 amexistentialdepress

    Could you please add one script or command line ,where we can use both Get-mailbox and Get-MailboxStatistics?

  10. on 17 Oct 2012 at 1:08 pmRose- Norfolk Va

    Thank you for the cool scripts. :)

  11. on 15 Apr 2013 at 9:32 amFaith quotes

    I added your blog to favorites Looking for more update.

Trackback this post | Feed on Comments to this post

Leave a Reply