Monday, February 16, 2015

Basic Exchange Management PowerShell Scripts

This page describes and lists various Exchange Mgmt Shell Scripts and how to use them. 

Document Creator 
Todd Walters    7/16/2010 


Requirements 
  • Exchange Management Shell -   
  • Windows PowerShell - Scripting tool from Microsoft. 
  • To add mailbox permissions for yourself use run-as to launch Exchange Mgmt Shell using the  someserviceacct  service account and enter this: 
  • Add-mailboxpermission -identity <user> -accessrights fullaccess -user <yourname> 
  1. note - Must Add Exchange Mgmt Shell SnapIn to PowerShell to Run Scripts on PowerShell 
  1. run this in PowerSherll: add-pssnapin Microsoft.Exchange.Management.PowerShell.Admin
  1. Microsoft.Exchange.Management.PowerShell.E2010
  1. Microsoft.Exchange.Management.PowerShell.Support

  1.  
  1. How to list mailbox user 
  1. GET-Mailbox –identity twalters  
  1. How to get mailbox statisics of user 
  1. Get-MailboxStatistics -identity twalters 
  1. set-MailboxStatistics - identity twalters | select DisplayName, TotalItemSize, TotalDeletedItemsize, DatabaseName | format-table 
  1. Get Mailbox Stats of Mailbox Server 
  1. get-mailboxstatistics -server SomeMailServer | Sort-Object TotalItemSize –Descending | ft DisplayName,TotalItemSize,ItemCount
  1.  
  1. How to Increase a mailbox storage size from defaults. Use the set-mailbox cmdlet. This increases and sets the warning, prohibit send, and prohibit send and receive limits to 750mb, 800mb, and 900mb respectively and the defaults are ignored. 
  1. set-mailbox -identity username -IssueWarningQuota 786432000 -ProhibitSendQuota 838860800 -ProhibitSendReceiveQuota  943718400 -UseDatabaseQuotaDefaults $false
  1. or 
  1. Set-Mailbox testmbx -UseDatabaseQuotaDefaults:$False -IssueWarningQuota 750MB -ProhibitSendQuota 800MB -ProhibitSendReceiveQuota 900MB

  1.  
  1. Set Mailbox to Default Limits 
  1. set-mailbox -identity username -UseDatabaseQuotaDefaults $true 
  1. Get-Queue -Filter {status -eq "retry"} 

  1. How to list all distribution groups in OU 
  1. get-distributiongroup -OrganizationalUnit "mydomain/My DL GROUPs"
  1.  
  1. How to list members of a Distribution Group 
  1. Get-DistributionGroupMember –identity “distro - some distrogroup" (replace " " w/any DG)  add | ft name, primarysmtpaddress | Export-CSV c:\members.csv to export to CSV 
  1. How to list a User’s Distribution Group Membership 
  1. How to Remove a Distribution Group 
  1. Remove-DistributionGroup -Identity "DG Name"
  1.  
  1. How to Remove a User from a Distribution Group 
  1. Remove-DistributionGroupMember -Identity "DG Name" -Member user@Mydomain.com 
  1. How to add permission to Manage a Distribution Group 
  1. Set-DistributionList DLName -ManagedBy user1@domain.com,user2@domain.com 
  1. get-content Distros.txt | foreach-object { set-group -id $_ -managedBy user1@mydom.comuser2@mydom.com } 
  1. Add-ADPermission -Identity distro -User username -AccessRights WriteProperty -Properties "Member"
  1.  
  1. How to export mailbox for archiving (use z1.ps1 from NetAdmin Script Repo
  1. $username = Read-Host "Enter Username"
  1. $servername = Read-Host "Enter the Servername for User's H: drive"
  1. $pathname = "\\" + $servername + "\" + $username + "$\data\other"
  1. Export-Mailbox -Identity $username -PSTFolderPath $pathname -Confirm:$false 
  • $addy = (get-mailbox twalters).PrimarySMTPAddress.toString() ; write-host $addy 
  • get-distributiongroup "Distro Name" | select ManagedBy 
  • dsquery group -samid mySrv0citrix_users | dsget group -members  
  User1
dsget succeeded
 






  samid
permissions 
Mailbox Storage and Statistics

Mailbox Queue 


Mailbox Distribution Lists

Various 

list primary smtp address 

See Managed By for Distro     
 
  
dsquery Group 
How to List Member of said group or dl. 
[PS] H:\>dsget group  -members "CN=XX,OU=YY,DC=12,DC=com" | dsget user -samid


This command allows other command to access the mailbox in all the child domains: 
Set-ADServerSettings -ViewEntireForest:$true 


Mailbox migration commands that will migrate any mailbox in any domain that is listed in the Alias column of a .CSV file but Suspend the move until you are ready to start: 
Set-ADServerSettings -ViewEntireForest:$true 
import-csv C:\TempUserMoves1.csv | foreach {New-MoveRequest -Identity $_.Alias -BadItemLimit 10 -suspend } | find multiple 

Mailbox migration commands that will migrate any mailbox in any domain thats listed in the Alias column of a .CSV file but Suspend the move at the end of the migration. Only works on Exchange 2010 to 2010: 
Set-ADServerSettings -ViewEntireForest:$true 
import-csv C:\Temp\UserMoves1.csv | foreach {New-MoveRequest -Identity $_.Alias -BadItemLimit 10 -SuspendWhenReadyToComplete} | find multiple 

Resume suspended move request: 
import-csv C:\Temp\UserMoves1.csv | foreach {Resume-MoveRequest -Identity $_.Alias -Confirm:$False} | find multiple 

To exclude mailbox database from provisioning , launch EMS and type: 
Set-MailboxDatabase -Identity  ussd-mbdb01 -IsExcludedFromProvisioning $true 

To re-enable the mailbox database for provisioning, launch EMS and type: 
Set-MailboxDatabase -Identity  ussd-mbdb01 -IsExcludedFromProvisioning $false 

A script that move all public folder content from one server to another server: 
cd 'C:\Program Files\Microsoft\Exchange Server\v14\Scripts' 
.\MoveAllReplicas.ps1 -Server ussd-prd-esmb01 -NewServer ussd-prd-esmb03 

The location were the Exchange 2010 Scrip's are Stored: 
cd 'C:\Program Files\Microsoft\Exchange Server\v14\Scripts' 

These are the commands I used to set the attributes on the conference  after they were migrated to Exchange 2010: 
import-csv C:\temp\roomlist.csv | foreach {Set-Mailbox -Identity $_.Alias -Type Room} 
import-csv C:\temp\roomlist.csv | foreach {Set-CalendarProcessing -Identity $_.Alias -AutomateProcessing AutoAccept -BookingWindowInDays 270} 

A command will export a mailbox to a PST file on a file share: (In order for these commands to work the [Exchange Servers] group must have full access to the folder you are righting to) 
Set-ADServerSettings -ViewEntireForest:$true 
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 
New-MailboxExportRequest -Mailbox Alias -FilePath "\\server1\folder\filename.pst

This command is used when create conference room to the Calendar reviewer for everyone using the Alias column of a .CSV file. Most push Enter twice for command to run: 
import-csv C:\Temp\ConfRoom.csv | foreach { $room =  $_.Alias + ':\calendar'  
                              Set-MailboxFolderPermission -Identity $room -AccessRights Reviewer -User "Default" } 

This command creates Room List DLs: 
New-DistributionGroup “Room Name” –RoomList –Members “Conference You would like to added to the DL” 

This command is used to check the health of the Content Index on the DBs. It must be ran from the mailbox server: 
Get-MailboxDatabaseCopyStatus | fl name, contentindexstate 

This command forces DAG fail over ofdatabase when it is Crawling: 
Move-ActiveMailboxDatabase USSD-MBDB08 -SkipClientExperienceChecks  

List Mailboxes by Size on a Database: 
Get-MailboxDatabase ussd-mbdb02 | Get-MailboxStatistics | Sort totalitemsize -desc | ft displayname, totalitemsize, itemcount 

Removes the Hidden from Address book using the Alias filed in a .CSV: 
import-csv C:\Temp\GoLive.csv | foreach {Set-Mailbox -Identity $_.Alias -HiddenFromAddressListsEnabled $false} | find multiple 

Set the Accept Messages to all if a DL is set to be the only group to have rights to send to a user based on the Alias filed in a .CSV: 
import-csv C:\Temp\GoLive2.csv | foreach {Set-Mailbox -Identity $_.Alias -AcceptMessagesOnlyFromDLMembers $NULL} | find multiple 

When creating a new Public Database you will need to include it has a replicate. This scrip will add a new Public Database as a replicate based on the server name to all of the public folders.  This command needs to be ran from a server that already contains a Public Database: 
cd 'C:\Program Files\Microsoft\Exchange Server\v14\Scripts' 
.\AddReplicaToPFRecursive.ps1 -TopPublicFolder "\" -ServerToAdd servername 
.\AddReplicaToPFRecursive.ps1 -TopPublicFolder "\NON_IPM_SUBTREE" -ServerToAdd servername 

Give the members of a DL Send on Behalf of right of the DL. You must have manager permissions to the group for this command to work and the group must be both a DL and a security group. 
Set-DistributionGroup -Identity "DL-Finance_Revenue" -GrantSendOnBehalfTo "DL-Finance_Revenue" 

This command shows you the size of a Database and how much free space is available in it. 
Get-MailboxDatabase "MailBoxServerName" -Status | Select-Object Server,Name,AvailableNewMailboxSpace,databasesize 

Exchange Mgmt Shell Links