{"id":645,"date":"2015-06-05T20:55:00","date_gmt":"2015-06-05T20:55:00","guid":{"rendered":"https:\/\/www.geekmungus.co.uk\/?p=645"},"modified":"2022-11-05T10:53:19","modified_gmt":"2022-11-05T10:53:19","slug":"microsoft-exchange-2013-powershell-useful-scripts","status":"publish","type":"post","link":"https:\/\/geekmungus.co.uk\/?p=645","title":{"rendered":"Microsoft Exchange 2013 PowerShell Useful Scripts"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">There&#8217;s some basic tasks which are good to automate, to save you some time, you can configure them to run as a scheduled task.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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: <strong>exchangeserviceuser<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To run a PowerShell script from a scheduled task you should create a scheduled task with the following settings:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>General<\/strong><\/td><\/tr><tr><td>Name:<\/td><td>&lt; different on each script&nbsp;&nbsp;&nbsp;&gt;<\/td><\/tr><tr><td>Location:<\/td><td><\/td><\/tr><tr><td>Use the following user account:<\/td><td>DOMAIN\\exchangeserviceuser&nbsp;&nbsp;Run whether user is logged on or not. (Ticked)Run within highest privileges. (Ticked)&nbsp;<\/td><\/tr><tr><td>Configure for:<\/td><td>Windows 7, Windows Server 2008 R2<\/td><\/tr><tr><td><strong>Actions<\/strong><\/td><\/tr><tr><td>Action:<\/td><td>Start a program<\/td><\/tr><tr><td>Program Script:<\/td><td>c:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe<\/td><\/tr><tr><td>Add Arguments (optional):<\/td><td>-command &#8220;. &#8216;c:\\Program Files\\Microsoft\\Exchange Server\\v15\\Bin\\RemoteExchange.ps1&#8217;; Connect-ExchangeServer -auto; c:\\scripts&nbsp;&lt;different on each script&gt;&nbsp;.ps1&#8221;<\/td><\/tr><tr><td>Start in (optional):<\/td><td>&lt; blank &gt;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The PowerShell scripts are shown below for each task:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>AddressBookPolicy.ps1<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>### AddressBookPolicy.ps1\n\n### This script sets the address book policy for all users.\n\n# Uncomment this line for testing within the Windows PowerShell ISE.\n\n#add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010\n\n$allmailbox = get-mailbox -ResultSize Unlimited -OrganizationalUnit \"OU=Users,DC=domain,DC=co,DC=uk\"\n\nWrite-Host \"Applying Address Book Policies to\"$allmailbox.count\"Staff accounts......\"\n\n$i = 0\n\nForeach ($item in $allmailbox) {\n\n    $i = $i + 1\n\n    Set-Mailbox -Identity ($item.alias) -AddressBookPolicy \"Address Book Policy\"\n\n}\n\nWrite-Host \"Processing Complete!\"\n\nWrite-Host \"\"\n\nWrite-Host $i \"Accounts Processed\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>MailboxPerm.ps1<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>### MailboxPerm.ps1\n\n### This script sets the calendar sharing permissions for all users.\n\n# Uncomment this line for testing within the Windows PowerShell ISE.\n\n#add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010\n\n$allmailbox=get-mailbox -ResultSize Unlimited -OrganizationalUnit \"OU=Users,DC=domain,DC=co,DC=uk\"\n\nForeach ($item in $allmailbox) {\n\n    Set-mailboxfolderpermission \u2013identity ($item.alias+\u2019:\\calendar\u2019) \u2013user Default \u2013Accessrights Reviewer\n\n   \n\n}\n\nSet-mailboxfolderpermission -identity (\"mtgfm1:\\calendar\") -user Default -Accessrights AvailabilityOnly\n\nSet-mailboxfolderpermission -identity (\"mtgfm2:\\calendar\") -user Default -Accessrights AvailabilityOnly\n\nSet-mailboxfolderpermission -identity (\"mtgfm3:\\calendar\") -user Default -Accessrights AvailabilityOnly\n\nSet-mailboxfolderpermission -identity (\"mtgfm4:\\calendar\") -user Default -Accessrights AvailabilityOnly<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>MailboxRetention.ps1<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>### MailboxRetention.ps1\n\n### This script sets the Archive and retention policy for all mailboxes.\n\n# Uncomment this line for testing within the Windows PowerShell ISE.\n\n#add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010\n\nGet-Mailbox -Resultsize Unlimited | Set-Mailbox -RetentionPolicy \"Archive and Retention Policy\"\n\nGet-Mailbox -Resultsize Unlimited | Set-Mailbox -ArchiveQuota 25GB -ArchiveWarningQuota 20GB<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>OWAMailboxPolicy.ps1<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Get-OWAMailboxPolicy | Set-OWAMailboxPolicy \u2013AllowOfflineOn NoComputers\n\nGet-OWAMailboxPolicy | Set-OWAMailboxPolicy -setphotoenabled:$false\n\nGet-CASMailbox -ResultSize Unlimited | Set-CASMailbox -OWAMailboxPolicy Default<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>StaffAutomaticCreateMailbox.ps1<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>### StaffAutomaticCreateMailbox.ps1\n\n### This script enables the mailbox on all users yet to have a mailbox enabled.\n\n# Uncomment this line for testing within the Windows PowerShell ISE.\n\n#add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010\n\n# Get a list of all the users within the OU within Active Directory.\n\n$allusers = Get-AdUser -Filter * -SearchBase \"OU=Users,DC=domain,DC=co,DC=uk\" |  select samaccountname\n\nWrite-Host \"\"\n\nWrite-Host \"Starting to process\" $allusers.count \"user accounts:\"\n\nWrite-Host \"\"\n\n$i = 0\n\nForeach ($item in $allusers) {\n\n    if (!(Get-Mailbox $item.samAccountName -ErrorAction SilentlyContinue)) {\n\n        Write-Host $item.samAccountName \"- No Mailbox Exists, Creating.....\"\n\n       \n\n        Enable-Mailbox -Identity $item.samAccountName\n\n        $i = $i + 1\n\n    } else {\n\n        Write-Host $item.samAccountName \"- Mailbox Exists\"\n\n    } \n\n}\n\nWrite-Host \"\"\n\nWrite-Host \"Account processing complete!\"\n\nWrite-Host $allusers.count \"user accounts processed,\" $i \"mailboxes created.\"<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>There&#8217;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 &#8230; <a title=\"Microsoft Exchange 2013 PowerShell Useful Scripts\" class=\"read-more\" href=\"https:\/\/geekmungus.co.uk\/?p=645\" aria-label=\"Read more about Microsoft Exchange 2013 PowerShell Useful Scripts\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[],"class_list":["post-645","post","type-post","status-publish","format-standard","hentry","category-microsoft-exchange"],"_links":{"self":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/645","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=645"}],"version-history":[{"count":1,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/645\/revisions"}],"predecessor-version":[{"id":1438,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/645\/revisions\/1438"}],"wp:attachment":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=645"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=645"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=645"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}