{"id":11,"date":"2014-12-10T20:24:15","date_gmt":"2014-12-10T20:24:15","guid":{"rendered":"http:\/\/onebyte.org\/blog\/?p=11"},"modified":"2015-08-27T02:47:14","modified_gmt":"2015-08-27T02:47:14","slug":"exchange-powershell","status":"publish","type":"post","link":"https:\/\/www.onebyte.org\/blog\/2014\/12\/10\/exchange-powershell\/","title":{"rendered":"Useful Exchange Powershell Commands"},"content":{"rendered":"<h1>Exchange 2007<\/h1>\n<p><strong>Exchange 2007 view database sizes<\/strong><\/p>\n<pre class=\"wrap:true lang:ps decode:true\">Get-MailboxDatabase | foreach-object {add-member -inputobject $_ -membertype noteproperty -name mailboxdbsizeinGB -value ([math]::Round(([int64](get-wmiobject cim_datafile -computername $_.server -filter ('name=''' + $_.edbfilepath.pathname.replace(\"\\\",\"\\\\\") + '''')).filesize \/ 1GB),2)) -passthru} |  Sort-Object mailboxdbsizeinGB -Descending | format-table identity,mailboxdbsizeinGB<\/pre>\n<p><strong>List all AD users in the domain and export to CSV:<\/strong><\/p>\n<pre class=\"wrap:true lang:ps decode:true \">Get-ADUser -filter * -properties emailAddress | Select SamAccountName | Export-CSV c:\\temp.csv -notype<\/pre>\n<h1>Exchange 2010<\/h1>\n<p><strong>Check if database is mounted:<\/strong><\/p>\n<pre class=\"wrap:true lang:ps decode:true \">get-mailboxdatabase -status | format-table Name,Mounted<\/pre>\n<p><strong>Check database name\/server\/size.<\/strong><\/p>\n<pre class=\"wrap:true lang:ps decode:true\">Get-MailboxDatabase -Status | select ServerName,Name,DatabaseSize<\/pre>\n<p><strong>Display the existing state of the queue (exports)<\/strong><\/p>\n<pre class=\"lang:ps decode:true \">get-mailboxexportrequest | get-mailboxexportrequeststatistics<\/pre>\n<p><strong>Display database that the user is a member of:<\/strong><\/p>\n<pre class=\"wrap:true lang:ps decode:true \">Get-MailboxDatabase -Status | select ServerName,Name,DatabaseSize<\/pre>\n<p><strong>Exports user &#8216;billybob01&#8217; contacts folders to PST.<\/strong><\/p>\n<pre class=\"wrap:true lang:ps decode:true \">new-mailboxexportrequest billybob01  -IncludeFolders \"\\Contacts\",\"\\Contacts\\Office Contacts\",\"\\Contacts\\Office Contacts\" -FilePath \\\\localhost\\pst\\contacts.pst<\/pre>\n<p><strong>Updating GAL and OAB {script}:<\/strong><\/p>\n<pre class=\"lang:ps decode:true\">Update-GlobalAddressList -Identity \"Default Global Address List\"\r\nGet-OfflineAddressBook | Update-OfflineAddressBook\r\nStart-Sleep -Seconds 1800\r\nget-ClientAccessServer | Update-FileDistributionService\r\n<\/pre>\n<p style=\"margin: 0in; font-family: Calibri; font-size: 11.0pt;\"><span style=\"font-weight: bold;\">EXCHANGE HEALTH SCRIPTS etc. <\/span><\/p>\n<p style=\"margin: 0in; font-family: Calibri; font-size: 11.0pt;\">Wondering how many log files are generated per server every minute? Quickly find out by typing:<\/p>\n<pre class=\"lang:default decode:true \">Get-MailboxDatabase -Server $env:ComputerName | ?{ %{$_.DatabaseCopies | ?{$_.ReplayLagTime -ne [TimeSpan]::Zero -And $_.HostServerName -eq $env:ComputerName} } } | %{ $count = 0; $MinT = [DateTime]::MaxValue; $MaxT = [DateTime]::MinValue; Get-ChildItem -Path $_.LogFolderPath -Filter \"*????.log\" | %{ $count = $count + 1; if($_.LastWriteTime -gt $MaxT){ $MaxT  = $_.LastWriteTime}; if($_.LastWriteTime -lt $MinT){ $MinT= $_.LastWriteTime} }; ($count \/ ($MaxT.Subtract($MinT)).TotalMinutes) } | Measure-Object -Min -Max -Ave<\/pre>\n<p style=\"margin: 0in; font-family: Calibri; font-size: 11.0pt; color: black;\"><span style=\"font-weight: bold;\">BACKUP STATUS<\/span><\/p>\n<p style=\"margin: 0in; font-family: Calibri; font-size: 11.0pt; color: black;\">To see the timestamps for the last successful backups on a database, run the command below\u2026<\/p>\n<pre class=\"lang:default decode:true \">Get-MailboxDatabase -server &lt;&lt;mailbox server name&gt;&gt; -status | ft name,last*<\/pre>\n<p style=\"margin: 0in; font-family: Calibri; font-size: 11.0pt; color: black;\"><span style=\"font-weight: bold;\">See all mailbox sizes in DB to CSV<\/span><\/p>\n<pre class=\"lang:default decode:true \">Get-MailboxStatistics -Database \"DB146\" | Select DisplayName, ItemCount, TotalItemSize | Sort-Object TotalItemSize -Descending | Export-CSV C:\\MBSizes.csv<\/pre>\n<p style=\"margin: 0in; font-family: Calibri; font-size: 11.0pt; color: black;\"><span style=\"font-weight: bold;\">If you leave out the \u2013server switch, it will give you every DB on every server.<\/span><\/p>\n<pre class=\"lang:default decode:true \">get-mailboxdatabase -status |ft name,last*<\/pre>\n<p style=\"margin: 0in; font-family: Calibri; font-size: 11.0pt; color: black;\"><span style=\"font-weight: bold;\">CONTENT INDEX<\/span><\/p>\n<pre class=\"lang:default decode:true \">Cd $exscripts\r\n\u00a0\r\nGet-MailboxDatabaseCopyStatus -server exmbx001 | Sort-Object \"Status\" -Descending\r\nGet-MailboxDatabaseCopyStatus -server exmbx002 | Sort-Object \"Status\" -Descending\r\nGet-MailboxDatabaseCopyStatus -server exmbx003 | Sort-Object \"Status\" -Descending\r\nGet-MailboxDatabaseCopyStatus -server exmbx004 | Sort-Object \"Status\" -Descending\r\nGet-MailboxDatabaseCopyStatus -server exmbx005 | Sort-Object \"Status\" -Descending\r\nGet-MailboxDatabaseCopyStatus -server exmbx006 | Sort-Object \"Status\" -Descending\r\nGet-MailboxDatabaseCopyStatus -server exmbx007 | Sort-Object \"Status\" -Descending\r\nGet-MailboxDatabaseCopyStatus -server exmbx008 | Sort-Object \"Status\" -Descending<\/pre>\n<p>For All<\/p>\n<pre class=\"lang:default decode:true \">(Get-DatabaseAvailabilityGroup) | ForEach {$_.Servers | ForEach {Get-MailboxDatabaseCopyStatus -Server $_}} | Sort-Object \"content index state\" \u2013Descending<\/pre>\n<p style=\"margin: 0in; font-family: Calibri; font-size: 11.0pt; color: black;\"><span style=\"font-weight: bold;\">To Update Content Index if not Healthy<\/span><\/p>\n<pre class=\"lang:default decode:true \">update-mailboxdatabasecopy -Identity db101\\EXMBX001\u00a0 \u2013CatalogOnly<\/pre>\n<p style=\"margin: 0in; font-family: Calibri; font-size: 11.0pt; color: black;\"><span style=\"font-weight: bold;\">BALANCE DATABASES ON DAGS<\/span><\/p>\n<pre class=\"lang:default decode:true \">Cd $exscripts<\/pre>\n<p style=\"margin: 0in; font-family: Calibri; font-size: 11.0pt; color: black;\"><span style=\"font-weight: bold;\">You can use the script to output a report of the current mailbox database distribution among the DAG members:<\/span><\/p>\n<pre class=\"lang:default decode:true \">.\\RedistributeActiveDatabases.ps1 \u2013DagName dag01 \u2013ShowDatabaseDistributionByServer | ft\r\n.\\RedistributeActiveDatabases.ps1 \u2013DagName dag02 \u2013ShowDatabaseDistributionByServer | ft<\/pre>\n<p style=\"margin: 0in; font-family: Calibri; font-size: 11.0pt; color: black;\"><span style=\"font-weight: bold;\">Use this script to balance<\/span><\/p>\n<pre class=\"lang:default decode:true \">.\\RedistributeActiveDatabases.ps1 -DagName dag01 \u2013BalanceDbsByActivationPreference -Confirm:$false\r\n.\\RedistributeActiveDatabases.ps1 -DagName dag02 \u2013BalanceDbsByActivationPreference -Confirm:$false<\/pre>\n<p style=\"margin: 0in; font-family: Calibri; font-size: 11.0pt; color: black;\"><span style=\"font-weight: bold;\">Check if databases are mounted:<\/span><\/p>\n<pre class=\"lang:default decode:true \">get-mailboxdatabase -status | format-table Name,Mounted<\/pre>\n<p style=\"margin: 0in; font-family: Calibri; font-size: 11.0pt;\"><span style=\"font-weight: bold;\">Check Mailbox Database WhiteSpace:<\/span><\/p>\n<pre class=\"lang:default decode:true \">Get-MailboxDatabase -Status | Select-Object Server,Name,AvailableNewMailboxSpace<\/pre>\n<p style=\"margin: 0in; font-family: Calibri; font-size: 11.0pt;\"><span style=\"font-weight: bold;\">Check mailbox size + dumpster size:<\/span><\/p>\n<pre class=\"lang:default decode:true\">Get-MailboxStatistics \u2013database\u00a0&lt;DataBase&gt;\u00a0| Sort-Object TotalItemSize -Descending | ft DisplayName,@{label=\"TotalItemSize(MB)\";expression={$_.TotalItemSize.Value.ToMB()}},@{label=\"TotalDeletedItemSize(MB)\";expression={$_.totaldeleteditemsize.Value.ToMB()}},ItemCount<\/pre>\n<p style=\"margin: 0in; font-family: Calibri; font-size: 11.0pt;\"><span style=\"font-weight: bold;\">Message Tracing<\/span><\/p>\n<pre class=\"lang:default decode:true\">get-exchangeserver | where {$_.ServerRole -like \"*HubTransport*\"} | get-messagetrackinglog -sender:EMAIL@DOMAIN.COM\r\nNote: this will query all of the hub servers<\/pre>\n<p style=\"margin: 0in; font-family: Calibri; font-size: 11.0pt;\">\n","protected":false},"excerpt":{"rendered":"<p>Exchange 2007 Exchange 2007 view database sizes Get-MailboxDatabase | foreach-object {add-member -inputobject $_ -membertype noteproperty -name mailboxdbsizeinGB -value ([math]::Round(([int64](get-wmiobject cim_datafile -computername $_.server -filter (&#8216;name=&#8221;&#8217; + $_.edbfilepath.pathname.replace(&#8220;\\&#8221;,&#8221;\\\\&#8221;) + &#8221;&#8221;)).filesize \/ 1GB),2)) -passthru} | Sort-Object mailboxdbsizeinGB -Descending | format-table identity,mailboxdbsizeinGB List all AD users in the domain and export to CSV: Get-ADUser [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":93,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","footnotes":""},"categories":[2,5],"tags":[],"class_list":["post-11","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","category-windows"],"uagb_featured_image_src":{"full":["https:\/\/www.onebyte.org\/blog\/wp-content\/uploads\/2014\/12\/powershell-logo.jpg",613,480,false],"thumbnail":["https:\/\/www.onebyte.org\/blog\/wp-content\/uploads\/2014\/12\/powershell-logo-150x150.jpg",150,150,true],"medium":["https:\/\/www.onebyte.org\/blog\/wp-content\/uploads\/2014\/12\/powershell-logo-300x235.jpg",300,235,true],"medium_large":["https:\/\/www.onebyte.org\/blog\/wp-content\/uploads\/2014\/12\/powershell-logo.jpg",613,480,false],"large":["https:\/\/www.onebyte.org\/blog\/wp-content\/uploads\/2014\/12\/powershell-logo.jpg",613,480,false],"1536x1536":["https:\/\/www.onebyte.org\/blog\/wp-content\/uploads\/2014\/12\/powershell-logo.jpg",613,480,false],"2048x2048":["https:\/\/www.onebyte.org\/blog\/wp-content\/uploads\/2014\/12\/powershell-logo.jpg",613,480,false],"post-thumbnail":["https:\/\/www.onebyte.org\/blog\/wp-content\/uploads\/2014\/12\/powershell-logo.jpg",613,480,false],"modality-logo":["https:\/\/www.onebyte.org\/blog\/wp-content\/uploads\/2014\/12\/powershell-logo.jpg",102,80,false]},"uagb_author_info":{"display_name":"Mr-Moo","author_link":"https:\/\/www.onebyte.org\/blog\/author\/ahess\/"},"uagb_comment_info":0,"uagb_excerpt":"Exchange 2007 Exchange 2007 view database sizes Get-MailboxDatabase | foreach-object {add-member -inputobject $_ -membertype noteproperty -name mailboxdbsizeinGB -value ([math]::Round(([int64](get-wmiobject cim_datafile -computername $_.server -filter ('name=''' + $_.edbfilepath.pathname.replace(\"\\\",\"\\\\\") + '''')).filesize \/ 1GB),2)) -passthru} | Sort-Object mailboxdbsizeinGB -Descending | format-table identity,mailboxdbsizeinGB List all AD users in the domain and export to CSV: Get-ADUser [&hellip;]","_links":{"self":[{"href":"https:\/\/www.onebyte.org\/blog\/wp-json\/wp\/v2\/posts\/11","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.onebyte.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.onebyte.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.onebyte.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.onebyte.org\/blog\/wp-json\/wp\/v2\/comments?post=11"}],"version-history":[{"count":2,"href":"https:\/\/www.onebyte.org\/blog\/wp-json\/wp\/v2\/posts\/11\/revisions"}],"predecessor-version":[{"id":53,"href":"https:\/\/www.onebyte.org\/blog\/wp-json\/wp\/v2\/posts\/11\/revisions\/53"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.onebyte.org\/blog\/wp-json\/wp\/v2\/media\/93"}],"wp:attachment":[{"href":"https:\/\/www.onebyte.org\/blog\/wp-json\/wp\/v2\/media?parent=11"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.onebyte.org\/blog\/wp-json\/wp\/v2\/categories?post=11"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.onebyte.org\/blog\/wp-json\/wp\/v2\/tags?post=11"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}