Microsoft Exchange 2013 PowerShell Useful Scripts

posted 5 Jun 2015, 05:50 by Tristan Self
There's some basic tasks which are good to automate, to save you some time, you can configure them to run as a scheduled task.

First step is to create a user account to run the scripts, this account should have access to change Exchange and change Active Directory, lets call the account: exchangeserviceuser.

To run a PowerShell script from a scheduled task you should create a scheduled task with the following settings:

General

Name:

< different on each script  >

Location:

\

Use the following user account:

DOMAIN\exchangeserviceuser  

Run whether user is logged on or not. (Ticked)

Run within highest privileges. (Ticked)

 

Configure for:

Windows 7, Windows Server 2008 R2

Actions

Action:

Start a program

Program Script:

c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Add Arguments (optional):

-command ". 'c:\Program Files\Microsoft\Exchange Server\v15\Bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; c:\scripts\<different on each script>.ps1"

Start in (optional):

< blank >


The PowerShell scripts are shown below for each task:

AddressBookPolicy.ps1

### AddressBookPolicy.ps1
### This script sets the address book policy for all users.

# Uncomment this line for testing within the Windows PowerShell ISE.
#add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010

$allmailbox = get-mailbox -ResultSize Unlimited -OrganizationalUnit "OU=Users,DC=domain,DC=co,DC=uk"

Write-Host "Applying Address Book Policies to"$allmailbox.count"Staff accounts......"

$i = 0

Foreach ($item in $allmailbox) {
    $i = $i + 1
    Set-Mailbox -Identity ($item.alias) -AddressBookPolicy "Address Book Policy"
}

Write-Host "Processing Complete!"
Write-Host ""
Write-Host $i "Accounts Processed"


MailboxPerm.ps1

### MailboxPerm.ps1
### This script sets the calendar sharing permissions for all users.

# Uncomment this line for testing within the Windows PowerShell ISE.
#add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010

$allmailbox=get-mailbox -ResultSize Unlimited -OrganizationalUnit "OU=Users,DC=domain,DC=co,DC=uk"
Foreach ($item in $allmailbox) {

    Set-mailboxfolderpermission –identity ($item.alias+’:\calendar’) –user Default –Accessrights Reviewer
   
}

Set-mailboxfolderpermission -identity ("mtgfm1:\calendar") -user Default -Accessrights AvailabilityOnly
Set-mailboxfolderpermission -identity ("mtgfm2:\calendar") -user Default -Accessrights AvailabilityOnly
Set-mailboxfolderpermission -identity ("mtgfm3:\calendar") -user Default -Accessrights AvailabilityOnly
Set-mailboxfolderpermission -identity ("mtgfm4:\calendar") -user Default -Accessrights AvailabilityOnly


MailboxRetention.ps1

### MailboxRetention.ps1
### This script sets the Archive and retention policy for all mailboxes.

# Uncomment this line for testing within the Windows PowerShell ISE.
#add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010

Get-Mailbox -Resultsize Unlimited | Set-Mailbox -RetentionPolicy "Oaklands Archive and Retention Policy"
Get-Mailbox -Resultsize Unlimited | Set-Mailbox -ArchiveQuota 25GB -ArchiveWarningQuota 20GB


OWAMailboxPolicy.ps1

Get-OWAMailboxPolicy | Set-OWAMailboxPolicy –AllowOfflineOn NoComputers
Get-OWAMailboxPolicy | Set-OWAMailboxPolicy -setphotoenabled:$false
Get-CASMailbox -ResultSize Unlimited | Set-CASMailbox -OWAMailboxPolicy Default


StaffAutomaticCreateMailbox.ps1

### StaffAutomaticCreateMailbox.ps1
### This script enables the mailbox on all users yet to have a mailbox enabled.

# Uncomment this line for testing within the Windows PowerShell ISE.
#add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010

# Get a list of all the users within the OU within Active Directory.
$allusers = Get-AdUser -Filter * -SearchBase "OU=Users,DC=domain,DC=co,DC=uk" |  select samaccountname

Write-Host ""
Write-Host "Starting to process" $allusers.count "user accounts:"
Write-Host ""

$i = 0

Foreach ($item in $allusers) {

    if (!(Get-Mailbox $item.samAccountName -ErrorAction SilentlyContinue)) {
        Write-Host $item.samAccountName "- No Mailbox Exists, Creating....."
       
        Enable-Mailbox -Identity $item.samAccountName

        $i = $i + 1
    } else {
        Write-Host $item.samAccountName "- Mailbox Exists"
    } 
}
Write-Host ""
Write-Host "Account processing complete!"
Write-Host $allusers.count "user accounts processed," $i "mailboxes created."



Comments