This script could be used to modify Office 365 mailbox permissions. You can use this script to add or remove mailbox permissions. This script will try to connect Windows PowerShell to Office 365 automatically if the connection is not established.
In a real world, IT Administrators may want to modify mailbox permissions due to variety of reasons. If an organization has thousands of mailboxes, it’s impossible to modify permissions for these mailboxes one by one. IT administrators do need a script to complete this task.
This script provides a graphic user interface (GUI). You can use this script in following way:
#requires -Version 2
#Import Localized Data
Import-LocalizedData -BindingVariable Messages
#Generated Form Function
function GenerateForm {
########################################################################
# Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.10.0
########################################################################
#region Import the Assemblies
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
#endregion
#region Generated Form Objects
$form1 = New-Object System.Windows.Forms.Form
$groupBox3 = New-Object System.Windows.Forms.GroupBox
$checkBoxWhatif = New-Object System.Windows.Forms.CheckBox
$radioButtonRemoveMbxPermission = New-Object System.Windows.Forms.RadioButton
$radioButtonAddMbxPermission = New-Object System.Windows.Forms.RadioButton
$buttonRun = New-Object System.Windows.Forms.Button
$groupBox2 = New-Object System.Windows.Forms.GroupBox
$radioButtonDeny = New-Object System.Windows.Forms.RadioButton
$radioButtonAllow = New-Object System.Windows.Forms.RadioButton
$textBoxUser = New-Object System.Windows.Forms.TextBox
$label5 = New-Object System.Windows.Forms.Label
$comboBoxAccessRights = New-Object System.Windows.Forms.ComboBox
$label4 = New-Object System.Windows.Forms.Label
$textboxMailboxFilter = New-Object System.Windows.Forms.TextBox
$label3 = New-Object System.Windows.Forms.Label
$groupBox1 = New-Object System.Windows.Forms.GroupBox
$textboxPassword = New-Object System.Windows.Forms.TextBox
$label2 = New-Object System.Windows.Forms.Label
$textBoxUserName = New-Object System.Windows.Forms.TextBox
$label1 = New-Object System.Windows.Forms.Label
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects
#----------------------------------------------
#Generated Event Script Blocks
#----------------------------------------------
#Provide Custom Code for events specified in PrimalForms.
$handler_form1_Load=
{
#Verify the connection between Windows PowerShell and Office 365
$existingSession = Get-PSSession -Verbose:$false | Where-Object {$_.ConfigurationName -eq "Microsoft.Exchange"}
if ($existingSession -ne $null) {
$form1.Text = $Messages.FormTextConnected
$textBoxUserName.Enabled = $false
$textBoxPassword.Enabled = $false
} else {
$form1.Text = $Messages.FormTextDisconnected
}
}
$handler_buttonRun_Click=
{
#Verify the parameters
$rawParams = @{}
$rawParams.Add("UserName",$textBoxUserName.Text)
$rawParams.Add("Password",$textboxPassword.Text)
$rawParams.Add("MailboxFilter",$textboxMailboxFilter.Text)
$rawParams.Add("User",$textboxUser.Text)
$rawParams.Add("AccessRights",$comboBoxAccessRights.Text)
foreach ($rawParam in $rawParams.GetEnumerator()) {
if ([System.String]::IsNullOrEmpty($rawParam.Value)) {
if ($existingSession -ne $null) {
if ($rawParam.Name -notmatch "UserName|Password") {
$errorMsgCaption = $Messages.InvalidParamsCaption
$errorMsg = $Messages.InvalidParamsMessage
$errorMsg = $errorMsg -replace "Placeholder01",$($rawParam.Name)
[System.Windows.Forms.MessageBox]::Show($errorMsg,$errorMsgCaption,"OK","Error")
$verifiedParams = $false
break
}
} else {
$errorMsgCaption = $Messages.InvalidParamsCaption
$errorMsg = $Messages.InvalidParamsMessage
$errorMsg = $errorMsg -replace "Placeholder01",$($rawParam.Name)
[System.Windows.Forms.MessageBox]::Show($errorMsg,$errorMsgCaption,"OK","Error")
$verifiedParams = $false
break
}
} else {
$verifiedParams = $true
}
}
#Connect Windows PowerShell to Office 365 if session does not exist
if ($verifiedParams) {
if ($existingSession -eq $null) {
$secuPwd = ConvertTo-SecureString -String $($textboxPassword.Text) -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($textBoxUserName.Text,$secuPwd)
Try
{
$existingSession = New-PSSession -ConfigurationName Microsoft.Exchange `
-ConnectionUri "https://ps.outlook.com/powershell" -Credential $cred `
-Authentication Basic -AllowRedirection
#If session is newly created, import the session.
if ($existingSession -ne $null) {
Import-PSSession -Session $existingSession -Verbose:$false | Out-Null
}
}
Catch
{
Write-Error $Error[0]
}
}
if ($existingSession -ne $null) {
#Change GUI objects to refelect session state
$form1.Text = $Messages.FormTextConnected
$textBoxUserName.Enabled = $false
$textBoxPassword.Enabled = $false
#Try to get mailboxes by using specified filter
Try
{
$mailboxes = Get-Mailbox -Filter $rawParams["MailboxFilter"] -ResultSize unlimited
}
Catch
{
Write-Error $Error[0]
}
#Begin to modify mailbox permission
if ($mailboxes -ne $null) {
$user = $rawParams["User"]
$accessRights = $rawParams["AccessRights"]
if ($radioButtonAllow.Checked) {
$cmdParams = "-User $user -AccessRights $accessRights"
} else {
$cmdParams = "-User $user -AccessRights $accessRights -Deny"
}
foreach ($mailbox in $mailboxes) {
if ($checkBoxWhatif.Checked) {
if ($radioButtonAddMbxPermission.Checked) {
$outputMsg = $Messages.InvokingCommand
$outputMsg = $outputMsg -replace "Placeholder01","Add-MailboxPermission -Identity $($mailbox.Alias) $cmdParams -Whatif"
Write-Host $outputMsg
Invoke-Expression -Command "Add-MailboxPermission -Identity $($mailbox.Alias) $cmdParams -Whatif"
} else {
$outputMsg = $Messages.InvokingCommand
$outputMsg = $outputMsg -replace "Placeholder01","Remove-MailboxPermission -Identity $($mailbox.Alias) $cmdParams -Whatif"
Write-Host $outputMsg
Invoke-Expression -Command "Remove-MailboxPermission -Identity $($mailbox.Alias) $cmdParams -Whatif"
}
} else {
if ($radioButtonAddMbxPermission.Checked) {
$outputMsg = $Messages.InvokingCommand
$outputMsg = $outputMsg -replace "Placeholder01","Add-MailboxPermission -Identity $($mailbox.Alias) $cmdParams"
Write-Host $outputMsg
Invoke-Expression -Command "Add-MailboxPermission -Identity $($mailbox.Alias) $cmdParams"
} else {
$outputMsg = $Messages.InvokingCommand
$outputMsg = $outputMsg -replace "Placeholder01","Remove-MailboxPermission -Identity $($mailbox.Alias) $cmdParams -Confirm:`$false"
Write-Host $outputMsg
Invoke-Expression -Command "Remove-MailboxPermission -Identity $($mailbox.Alias) $cmdParams -Confirm:`$false"
}
}
}
if (-not $checkBoxWhatif.Checked){
#Reset GUI objects to initial state
$textboxMailboxFilter.Text = ""
$textBoxUser.Text = ""
$comboBoxAccessRights.ResetText()
$radioButtonAllow.Checked = $true
$radioButtonAddMbxPermission.Checked = $true
$checkBoxWhatif.Checked = $true
}
} else {
$errorMsgCaption = $Messages.CannotFindMailboxCaption
$errorMsg = $Messages.CannotFindMailboxMessage
[System.Windows.Forms.MessageBox]::Show($errorMsg,$errorMsgCaption,"OK","Error")
}
}
}
}
$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
$form1.WindowState = $InitialFormWindowState
}
#----------------------------------------------
#region Generated Form Code
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 291
$System_Drawing_Size.Width = 594
$form1.ClientSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.FormBorderStyle = 1
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = -15
$System_Drawing_Point.Y = -134
$form1.Location = $System_Drawing_Point
$form1.MaximizeBox = $False
$form1.Name = "form1"
$form1.Text = "Modify Office 365 Mailbox Permission"
$form1.add_Load($handler_form1_Load)
$groupBox3.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 203
$groupBox3.Location = $System_Drawing_Point
$groupBox3.Name = "groupBox3"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 50
$System_Drawing_Size.Width = 570
$groupBox3.Size = $System_Drawing_Size
$groupBox3.TabIndex = 27
$groupBox3.TabStop = $False
$groupBox3.Text = "Operation"
$form1.Controls.Add($groupBox3)
$checkBoxWhatif.AutoSize = $True
$checkBoxWhatif.Checked = $True
$checkBoxWhatif.CheckState = 1
$checkBoxWhatif.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 348
$System_Drawing_Point.Y = 20
$checkBoxWhatif.Location = $System_Drawing_Point
$checkBoxWhatif.Name = "checkBoxWhatif"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 17
$System_Drawing_Size.Width = 60
$checkBoxWhatif.Size = $System_Drawing_Size
$checkBoxWhatif.TabIndex = 10
$checkBoxWhatif.Text = "-Whatif"
$checkBoxWhatif.UseVisualStyleBackColor = $True
$groupBox3.Controls.Add($checkBoxWhatif)
$radioButtonRemoveMbxPermission.AutoSize = $True
$radioButtonRemoveMbxPermission.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 162
$System_Drawing_Point.Y = 19
$radioButtonRemoveMbxPermission.Location = $System_Drawing_Point
$radioButtonRemoveMbxPermission.Name = "radioButtonRemoveMbxPermission"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 17
$System_Drawing_Size.Width = 157
$radioButtonRemoveMbxPermission.Size = $System_Drawing_Size
$radioButtonRemoveMbxPermission.TabIndex = 9
$radioButtonRemoveMbxPermission.Text = "Remove Mailbox Permission"
$radioButtonRemoveMbxPermission.UseVisualStyleBackColor = $True
$groupBox3.Controls.Add($radioButtonRemoveMbxPermission)
$radioButtonAddMbxPermission.AutoSize = $True
$radioButtonAddMbxPermission.Checked = $True
$radioButtonAddMbxPermission.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 8
$System_Drawing_Point.Y = 19
$radioButtonAddMbxPermission.Location = $System_Drawing_Point
$radioButtonAddMbxPermission.Name = "radioButtonAddMbxPermission"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 17
$System_Drawing_Size.Width = 136
$radioButtonAddMbxPermission.Size = $System_Drawing_Size
$radioButtonAddMbxPermission.TabIndex = 8
$radioButtonAddMbxPermission.TabStop = $True
$radioButtonAddMbxPermission.Text = "Add Mailbox Permission"
$radioButtonAddMbxPermission.UseVisualStyleBackColor = $True
$groupBox3.Controls.Add($radioButtonAddMbxPermission)
$buttonRun.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 256
$System_Drawing_Point.Y = 259
$buttonRun.Location = $System_Drawing_Point
$buttonRun.Name = "buttonRun"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 75
$buttonRun.Size = $System_Drawing_Size
$buttonRun.TabIndex = 11
$buttonRun.Text = "Run"
$buttonRun.UseVisualStyleBackColor = $True
$buttonRun.add_Click($handler_buttonRun_Click)
$form1.Controls.Add($buttonRun)
$groupBox2.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 91
$groupBox2.Location = $System_Drawing_Point
$groupBox2.Name = "groupBox2"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 106
$System_Drawing_Size.Width = 570
$groupBox2.Size = $System_Drawing_Size
$groupBox2.TabIndex = 23
$groupBox2.TabStop = $False
$groupBox2.Text = "Parameters"
$form1.Controls.Add($groupBox2)
$radioButtonDeny.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 458
$System_Drawing_Point.Y = 72
$radioButtonDeny.Location = $System_Drawing_Point
$radioButtonDeny.Name = "radioButtonDeny"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 24
$System_Drawing_Size.Width = 104
$radioButtonDeny.Size = $System_Drawing_Size
$radioButtonDeny.TabIndex = 7
$radioButtonDeny.Text = "Deny"
$radioButtonDeny.UseVisualStyleBackColor = $True
$groupBox2.Controls.Add($radioButtonDeny)
$radioButtonAllow.Checked = $True
$radioButtonAllow.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 348
$System_Drawing_Point.Y = 72
$radioButtonAllow.Location = $System_Drawing_Point
$radioButtonAllow.Name = "radioButtonAllow"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 24
$System_Drawing_Size.Width = 104
$radioButtonAllow.Size = $System_Drawing_Size
$radioButtonAllow.TabIndex = 6
$radioButtonAllow.TabStop = $True
$radioButtonAllow.Text = "Allow"
$radioButtonAllow.UseVisualStyleBackColor = $True
$groupBox2.Controls.Add($radioButtonAllow)
$textBoxUser.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 87
$System_Drawing_Point.Y = 46
$textBoxUser.Location = $System_Drawing_Point
$textBoxUser.Name = "textBoxUser"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 475
$textBoxUser.Size = $System_Drawing_Size
$textBoxUser.TabIndex = 4
$groupBox2.Controls.Add($textBoxUser)
$label5.AutoSize = $True
$label5.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 7
$System_Drawing_Point.Y = 75
$label5.Location = $System_Drawing_Point
$label5.Name = "label5"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 13
$System_Drawing_Size.Width = 78
$label5.Size = $System_Drawing_Size
$label5.TabIndex = 26
$label5.Text = "Access Rights:"
$groupBox2.Controls.Add($label5)
$comboBoxAccessRights.DataBindings.DefaultDataSourceUpdateMode = 0
$comboBoxAccessRights.DropDownHeight = 110
$comboBoxAccessRights.DropDownStyle = 2
$comboBoxAccessRights.DropDownWidth = 190
$comboBoxAccessRights.FormattingEnabled = $True
$comboBoxAccessRights.IntegralHeight = $False
$comboBoxAccessRights.Items.Add("")|Out-Null
$comboBoxAccessRights.Items.Add("FullAccess")|Out-Null
$comboBoxAccessRights.Items.Add("ExternalAccount")|Out-Null
$comboBoxAccessRights.Items.Add("DeleteItem")|Out-Null
$comboBoxAccessRights.Items.Add("ReadPermission")|Out-Null
$comboBoxAccessRights.Items.Add("ChangePermission")|Out-Null
$comboBoxAccessRights.Items.Add("ChangeOwner")|Out-Null
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 87
$System_Drawing_Point.Y = 72
$comboBoxAccessRights.Location = $System_Drawing_Point
$comboBoxAccessRights.Name = "comboBoxAccessRights"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 21
$System_Drawing_Size.Width = 232
$comboBoxAccessRights.Size = $System_Drawing_Size
$comboBoxAccessRights.TabIndex = 5
$groupBox2.Controls.Add($comboBoxAccessRights)
$label4.AutoSize = $True
$label4.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 7
$System_Drawing_Point.Y = 49
$label4.Location = $System_Drawing_Point
$label4.Name = "label4"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 13
$System_Drawing_Size.Width = 32
$label4.Size = $System_Drawing_Size
$label4.TabIndex = 25
$label4.Text = "User (Alias):"
$groupBox2.Controls.Add($label4)
$textboxMailboxFilter.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 87
$System_Drawing_Point.Y = 19
$textboxMailboxFilter.Location = $System_Drawing_Point
$textboxMailboxFilter.Name = "textboxMailboxFilter"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 475
$textboxMailboxFilter.Size = $System_Drawing_Size
$textboxMailboxFilter.TabIndex = 3
$groupBox2.Controls.Add($textboxMailboxFilter)
$label3.AutoSize = $True
$label3.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 7
$System_Drawing_Point.Y = 22
$label3.Location = $System_Drawing_Point
$label3.Name = "label3"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 13
$System_Drawing_Size.Width = 71
$label3.Size = $System_Drawing_Size
$label3.TabIndex = 24
$label3.Text = "Mailbox Filter:"
$groupBox2.Controls.Add($label3)
$groupBox1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 12
$groupBox1.Location = $System_Drawing_Point
$groupBox1.Name = "groupBox1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 73
$System_Drawing_Size.Width = 570
$groupBox1.Size = $System_Drawing_Size
$groupBox1.TabIndex = 20
$groupBox1.TabStop = $False
$groupBox1.Text = "Credential for Connecting Office 365"
$form1.Controls.Add($groupBox1)
$textboxPassword.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 87
$System_Drawing_Point.Y = 41
$textboxPassword.Location = $System_Drawing_Point
$textboxPassword.Name = "textboxPassword"
$textboxPassword.PasswordChar = '*'
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 475
$textboxPassword.Size = $System_Drawing_Size
$textboxPassword.TabIndex = 2
$groupBox1.Controls.Add($textboxPassword)
$label2.AutoSize = $True
$label2.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 6
$System_Drawing_Point.Y = 41
$label2.Location = $System_Drawing_Point
$label2.Name = "label2"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 13
$System_Drawing_Size.Width = 56
$label2.Size = $System_Drawing_Size
$label2.TabIndex = 22
$label2.Text = "Password:"
$groupBox1.Controls.Add($label2)
$textBoxUserName.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 87
$System_Drawing_Point.Y = 16
$textBoxUserName.Location = $System_Drawing_Point
$textBoxUserName.Name = "textBoxUserName"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 475
$textBoxUserName.Size = $System_Drawing_Size
$textBoxUserName.TabIndex = 1
$groupBox1.Controls.Add($textBoxUserName)
$label1.AutoSize = $True
$label1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 6
$System_Drawing_Point.Y = 19
$label1.Location = $System_Drawing_Point
$label1.Name = "label1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 13
$System_Drawing_Size.Width = 63
$label1.Size = $System_Drawing_Size
$label1.TabIndex = 21
$label1.Text = "User Name:"
$groupBox1.Controls.Add($label1)
#endregion Generated Form Code
#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$form1.ShowDialog()| Out-Null
} #End Function
#Call the Function
GenerateForm
#requires -Version 2 #Import Localized Data Import-LocalizedData -BindingVariable Messages #Generated Form Function function GenerateForm { ######################################################################## # Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.10.0 ######################################################################## #region Import the Assemblies Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing #endregion #region Generated Form Objects $form1 = New-Object System.Windows.Forms.Form $groupBox3 = New-Object System.Windows.Forms.GroupBox $checkBoxWhatif = New-Object System.Windows.Forms.CheckBox $radioButtonRemoveMbxPermission = New-Object System.Windows.Forms.RadioButton $radioButtonAddMbxPermission = New-Object System.Windows.Forms.RadioButton $buttonRun = New-Object System.Windows.Forms.Button $groupBox2 = New-Object System.Windows.Forms.GroupBox $radioButtonDeny = New-Object System.Windows.Forms.RadioButton $radioButtonAllow = New-Object System.Windows.Forms.RadioButton $textBoxUser = New-Object System.Windows.Forms.TextBox $label5 = New-Object System.Windows.Forms.Label $comboBoxAccessRights = New-Object System.Windows.Forms.ComboBox $label4 = New-Object System.Windows.Forms.Label $textboxMailboxFilter = New-Object System.Windows.Forms.TextBox $label3 = New-Object System.Windows.Forms.Label $groupBox1 = New-Object System.Windows.Forms.GroupBox $textboxPassword = New-Object System.Windows.Forms.TextBox $label2 = New-Object System.Windows.Forms.Label $textBoxUserName = New-Object System.Windows.Forms.TextBox $label1 = New-Object System.Windows.Forms.Label $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState #endregion Generated Form Objects #---------------------------------------------- #Generated Event Script Blocks #---------------------------------------------- #Provide Custom Code for events specified in PrimalForms. $handler_form1_Load= { #Verify the connection between Windows PowerShell and Office 365 $existingSession = Get-PSSession -Verbose:$false | Where-Object {$_.ConfigurationName -eq "Microsoft.Exchange"} if ($existingSession -ne $null) { $form1.Text = $Messages.FormTextConnected $textBoxUserName.Enabled = $false $textBoxPassword.Enabled = $false } else { $form1.Text = $Messages.FormTextDisconnected } } $handler_buttonRun_Click= { #Verify the parameters $rawParams = @{} $rawParams.Add("UserName",$textBoxUserName.Text) $rawParams.Add("Password",$textboxPassword.Text) $rawParams.Add("MailboxFilter",$textboxMailboxFilter.Text) $rawParams.Add("User",$textboxUser.Text) $rawParams.Add("AccessRights",$comboBoxAccessRights.Text) foreach ($rawParam in $rawParams.GetEnumerator()) { if ([System.String]::IsNullOrEmpty($rawParam.Value)) { if ($existingSession -ne $null) { if ($rawParam.Name -notmatch "UserName|Password") { $errorMsgCaption = $Messages.InvalidParamsCaption $errorMsg = $Messages.InvalidParamsMessage $errorMsg = $errorMsg -replace "Placeholder01",$($rawParam.Name) [System.Windows.Forms.MessageBox]::Show($errorMsg,$errorMsgCaption,"OK","Error") $verifiedParams = $false break } } else { $errorMsgCaption = $Messages.InvalidParamsCaption $errorMsg = $Messages.InvalidParamsMessage $errorMsg = $errorMsg -replace "Placeholder01",$($rawParam.Name) [System.Windows.Forms.MessageBox]::Show($errorMsg,$errorMsgCaption,"OK","Error") $verifiedParams = $false break } } else { $verifiedParams = $true } } #Connect Windows PowerShell to Office 365 if session does not exist if ($verifiedParams) { if ($existingSession -eq $null) { $secuPwd = ConvertTo-SecureString -String $($textboxPassword.Text) -AsPlainText -Force $cred = New-Object System.Management.Automation.PSCredential($textBoxUserName.Text,$secuPwd) Try { $existingSession = New-PSSession -ConfigurationName Microsoft.Exchange ` -ConnectionUri "https://ps.outlook.com/powershell" -Credential $cred ` -Authentication Basic -AllowRedirection #If session is newly created, import the session. if ($existingSession -ne $null) { Import-PSSession -Session $existingSession -Verbose:$false | Out-Null } } Catch { Write-Error $Error[0] } } if ($existingSession -ne $null) { #Change GUI objects to refelect session state $form1.Text = $Messages.FormTextConnected $textBoxUserName.Enabled = $false $textBoxPassword.Enabled = $false #Try to get mailboxes by using specified filter Try { $mailboxes = Get-Mailbox -Filter $rawParams["MailboxFilter"] -ResultSize unlimited } Catch { Write-Error $Error[0] } #Begin to modify mailbox permission if ($mailboxes -ne $null) { $user = $rawParams["User"] $accessRights = $rawParams["AccessRights"] if ($radioButtonAllow.Checked) { $cmdParams = "-User $user -AccessRights $accessRights" } else { $cmdParams = "-User $user -AccessRights $accessRights -Deny" } foreach ($mailbox in $mailboxes) { if ($checkBoxWhatif.Checked) { if ($radioButtonAddMbxPermission.Checked) { $outputMsg = $Messages.InvokingCommand $outputMsg = $outputMsg -replace "Placeholder01","Add-MailboxPermission -Identity $($mailbox.Alias) $cmdParams -Whatif" Write-Host $outputMsg Invoke-Expression -Command "Add-MailboxPermission -Identity $($mailbox.Alias) $cmdParams -Whatif" } else { $outputMsg = $Messages.InvokingCommand $outputMsg = $outputMsg -replace "Placeholder01","Remove-MailboxPermission -Identity $($mailbox.Alias) $cmdParams -Whatif" Write-Host $outputMsg Invoke-Expression -Command "Remove-MailboxPermission -Identity $($mailbox.Alias) $cmdParams -Whatif" } } else { if ($radioButtonAddMbxPermission.Checked) { $outputMsg = $Messages.InvokingCommand $outputMsg = $outputMsg -replace "Placeholder01","Add-MailboxPermission -Identity $($mailbox.Alias) $cmdParams" Write-Host $outputMsg Invoke-Expression -Command "Add-MailboxPermission -Identity $($mailbox.Alias) $cmdParams" } else { $outputMsg = $Messages.InvokingCommand $outputMsg = $outputMsg -replace "Placeholder01","Remove-MailboxPermission -Identity $($mailbox.Alias) $cmdParams -Confirm:`$false" Write-Host $outputMsg Invoke-Expression -Command "Remove-MailboxPermission -Identity $($mailbox.Alias) $cmdParams -Confirm:`$false" } } } if (-not $checkBoxWhatif.Checked){ #Reset GUI objects to initial state $textboxMailboxFilter.Text = "" $textBoxUser.Text = "" $comboBoxAccessRights.ResetText() $radioButtonAllow.Checked = $true $radioButtonAddMbxPermission.Checked = $true $checkBoxWhatif.Checked = $true } } else { $errorMsgCaption = $Messages.CannotFindMailboxCaption $errorMsg = $Messages.CannotFindMailboxMessage [System.Windows.Forms.MessageBox]::Show($errorMsg,$errorMsgCaption,"OK","Error") } } } } $OnLoadForm_StateCorrection= {#Correct the initial state of the form to prevent the .Net maximized form issue $form1.WindowState = $InitialFormWindowState } #---------------------------------------------- #region Generated Form Code $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Height = 291 $System_Drawing_Size.Width = 594 $form1.ClientSize = $System_Drawing_Size $form1.DataBindings.DefaultDataSourceUpdateMode = 0 $form1.FormBorderStyle = 1 $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = -15 $System_Drawing_Point.Y = -134 $form1.Location = $System_Drawing_Point $form1.MaximizeBox = $False $form1.Name = "form1" $form1.Text = "Modify Office 365 Mailbox Permission" $form1.add_Load($handler_form1_Load) $groupBox3.DataBindings.DefaultDataSourceUpdateMode = 0 $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 12 $System_Drawing_Point.Y = 203 $groupBox3.Location = $System_Drawing_Point $groupBox3.Name = "groupBox3" $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Height = 50 $System_Drawing_Size.Width = 570 $groupBox3.Size = $System_Drawing_Size $groupBox3.TabIndex = 27 $groupBox3.TabStop = $False $groupBox3.Text = "Operation" $form1.Controls.Add($groupBox3) $checkBoxWhatif.AutoSize = $True $checkBoxWhatif.Checked = $True $checkBoxWhatif.CheckState = 1 $checkBoxWhatif.DataBindings.DefaultDataSourceUpdateMode = 0 $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 348 $System_Drawing_Point.Y = 20 $checkBoxWhatif.Location = $System_Drawing_Point $checkBoxWhatif.Name = "checkBoxWhatif" $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Height = 17 $System_Drawing_Size.Width = 60 $checkBoxWhatif.Size = $System_Drawing_Size $checkBoxWhatif.TabIndex = 10 $checkBoxWhatif.Text = "-Whatif" $checkBoxWhatif.UseVisualStyleBackColor = $True $groupBox3.Controls.Add($checkBoxWhatif) $radioButtonRemoveMbxPermission.AutoSize = $True $radioButtonRemoveMbxPermission.DataBindings.DefaultDataSourceUpdateMode = 0 $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 162 $System_Drawing_Point.Y = 19 $radioButtonRemoveMbxPermission.Location = $System_Drawing_Point $radioButtonRemoveMbxPermission.Name = "radioButtonRemoveMbxPermission" $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Height = 17 $System_Drawing_Size.Width = 157 $radioButtonRemoveMbxPermission.Size = $System_Drawing_Size $radioButtonRemoveMbxPermission.TabIndex = 9 $radioButtonRemoveMbxPermission.Text = "Remove Mailbox Permission" $radioButtonRemoveMbxPermission.UseVisualStyleBackColor = $True $groupBox3.Controls.Add($radioButtonRemoveMbxPermission) $radioButtonAddMbxPermission.AutoSize = $True $radioButtonAddMbxPermission.Checked = $True $radioButtonAddMbxPermission.DataBindings.DefaultDataSourceUpdateMode = 0 $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 8 $System_Drawing_Point.Y = 19 $radioButtonAddMbxPermission.Location = $System_Drawing_Point $radioButtonAddMbxPermission.Name = "radioButtonAddMbxPermission" $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Height = 17 $System_Drawing_Size.Width = 136 $radioButtonAddMbxPermission.Size = $System_Drawing_Size $radioButtonAddMbxPermission.TabIndex = 8 $radioButtonAddMbxPermission.TabStop = $True $radioButtonAddMbxPermission.Text = "Add Mailbox Permission" $radioButtonAddMbxPermission.UseVisualStyleBackColor = $True $groupBox3.Controls.Add($radioButtonAddMbxPermission) $buttonRun.DataBindings.DefaultDataSourceUpdateMode = 0 $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 256 $System_Drawing_Point.Y = 259 $buttonRun.Location = $System_Drawing_Point $buttonRun.Name = "buttonRun" $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Height = 23 $System_Drawing_Size.Width = 75 $buttonRun.Size = $System_Drawing_Size $buttonRun.TabIndex = 11 $buttonRun.Text = "Run" $buttonRun.UseVisualStyleBackColor = $True $buttonRun.add_Click($handler_buttonRun_Click) $form1.Controls.Add($buttonRun) $groupBox2.DataBindings.DefaultDataSourceUpdateMode = 0 $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 12 $System_Drawing_Point.Y = 91 $groupBox2.Location = $System_Drawing_Point $groupBox2.Name = "groupBox2" $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Height = 106 $System_Drawing_Size.Width = 570 $groupBox2.Size = $System_Drawing_Size $groupBox2.TabIndex = 23 $groupBox2.TabStop = $False $groupBox2.Text = "Parameters" $form1.Controls.Add($groupBox2) $radioButtonDeny.DataBindings.DefaultDataSourceUpdateMode = 0 $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 458 $System_Drawing_Point.Y = 72 $radioButtonDeny.Location = $System_Drawing_Point $radioButtonDeny.Name = "radioButtonDeny" $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Height = 24 $System_Drawing_Size.Width = 104 $radioButtonDeny.Size = $System_Drawing_Size $radioButtonDeny.TabIndex = 7 $radioButtonDeny.Text = "Deny" $radioButtonDeny.UseVisualStyleBackColor = $True $groupBox2.Controls.Add($radioButtonDeny) $radioButtonAllow.Checked = $True $radioButtonAllow.DataBindings.DefaultDataSourceUpdateMode = 0 $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 348 $System_Drawing_Point.Y = 72 $radioButtonAllow.Location = $System_Drawing_Point $radioButtonAllow.Name = "radioButtonAllow" $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Height = 24 $System_Drawing_Size.Width = 104 $radioButtonAllow.Size = $System_Drawing_Size $radioButtonAllow.TabIndex = 6 $radioButtonAllow.TabStop = $True $radioButtonAllow.Text = "Allow" $radioButtonAllow.UseVisualStyleBackColor = $True $groupBox2.Controls.Add($radioButtonAllow) $textBoxUser.DataBindings.DefaultDataSourceUpdateMode = 0 $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 87 $System_Drawing_Point.Y = 46 $textBoxUser.Location = $System_Drawing_Point $textBoxUser.Name = "textBoxUser" $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Height = 20 $System_Drawing_Size.Width = 475 $textBoxUser.Size = $System_Drawing_Size $textBoxUser.TabIndex = 4 $groupBox2.Controls.Add($textBoxUser) $label5.AutoSize = $True $label5.DataBindings.DefaultDataSourceUpdateMode = 0 $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 7 $System_Drawing_Point.Y = 75 $label5.Location = $System_Drawing_Point $label5.Name = "label5" $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Height = 13 $System_Drawing_Size.Width = 78 $label5.Size = $System_Drawing_Size $label5.TabIndex = 26 $label5.Text = "Access Rights:" $groupBox2.Controls.Add($label5) $comboBoxAccessRights.DataBindings.DefaultDataSourceUpdateMode = 0 $comboBoxAccessRights.DropDownHeight = 110 $comboBoxAccessRights.DropDownStyle = 2 $comboBoxAccessRights.DropDownWidth = 190 $comboBoxAccessRights.FormattingEnabled = $True $comboBoxAccessRights.IntegralHeight = $False $comboBoxAccessRights.Items.Add("")|Out-Null $comboBoxAccessRights.Items.Add("FullAccess")|Out-Null $comboBoxAccessRights.Items.Add("ExternalAccount")|Out-Null $comboBoxAccessRights.Items.Add("DeleteItem")|Out-Null $comboBoxAccessRights.Items.Add("ReadPermission")|Out-Null $comboBoxAccessRights.Items.Add("ChangePermission")|Out-Null $comboBoxAccessRights.Items.Add("ChangeOwner")|Out-Null $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 87 $System_Drawing_Point.Y = 72 $comboBoxAccessRights.Location = $System_Drawing_Point $comboBoxAccessRights.Name = "comboBoxAccessRights" $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Height = 21 $System_Drawing_Size.Width = 232 $comboBoxAccessRights.Size = $System_Drawing_Size $comboBoxAccessRights.TabIndex = 5 $groupBox2.Controls.Add($comboBoxAccessRights) $label4.AutoSize = $True $label4.DataBindings.DefaultDataSourceUpdateMode = 0 $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 7 $System_Drawing_Point.Y = 49 $label4.Location = $System_Drawing_Point $label4.Name = "label4" $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Height = 13 $System_Drawing_Size.Width = 32 $label4.Size = $System_Drawing_Size $label4.TabIndex = 25 $label4.Text = "User (Alias):" $groupBox2.Controls.Add($label4) $textboxMailboxFilter.DataBindings.DefaultDataSourceUpdateMode = 0 $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 87 $System_Drawing_Point.Y = 19 $textboxMailboxFilter.Location = $System_Drawing_Point $textboxMailboxFilter.Name = "textboxMailboxFilter" $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Height = 20 $System_Drawing_Size.Width = 475 $textboxMailboxFilter.Size = $System_Drawing_Size $textboxMailboxFilter.TabIndex = 3 $groupBox2.Controls.Add($textboxMailboxFilter) $label3.AutoSize = $True $label3.DataBindings.DefaultDataSourceUpdateMode = 0 $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 7 $System_Drawing_Point.Y = 22 $label3.Location = $System_Drawing_Point $label3.Name = "label3" $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Height = 13 $System_Drawing_Size.Width = 71 $label3.Size = $System_Drawing_Size $label3.TabIndex = 24 $label3.Text = "Mailbox Filter:" $groupBox2.Controls.Add($label3) $groupBox1.DataBindings.DefaultDataSourceUpdateMode = 0 $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 12 $System_Drawing_Point.Y = 12 $groupBox1.Location = $System_Drawing_Point $groupBox1.Name = "groupBox1" $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Height = 73 $System_Drawing_Size.Width = 570 $groupBox1.Size = $System_Drawing_Size $groupBox1.TabIndex = 20 $groupBox1.TabStop = $False $groupBox1.Text = "Credential for Connecting Office 365" $form1.Controls.Add($groupBox1) $textboxPassword.DataBindings.DefaultDataSourceUpdateMode = 0 $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 87 $System_Drawing_Point.Y = 41 $textboxPassword.Location = $System_Drawing_Point $textboxPassword.Name = "textboxPassword" $textboxPassword.PasswordChar = '*' $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Height = 20 $System_Drawing_Size.Width = 475 $textboxPassword.Size = $System_Drawing_Size $textboxPassword.TabIndex = 2 $groupBox1.Controls.Add($textboxPassword) $label2.AutoSize = $True $label2.DataBindings.DefaultDataSourceUpdateMode = 0 $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 6 $System_Drawing_Point.Y = 41 $label2.Location = $System_Drawing_Point $label2.Name = "label2" $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Height = 13 $System_Drawing_Size.Width = 56 $label2.Size = $System_Drawing_Size $label2.TabIndex = 22 $label2.Text = "Password:" $groupBox1.Controls.Add($label2) $textBoxUserName.DataBindings.DefaultDataSourceUpdateMode = 0 $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 87 $System_Drawing_Point.Y = 16 $textBoxUserName.Location = $System_Drawing_Point $textBoxUserName.Name = "textBoxUserName" $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Height = 20 $System_Drawing_Size.Width = 475 $textBoxUserName.Size = $System_Drawing_Size $textBoxUserName.TabIndex = 1 $groupBox1.Controls.Add($textBoxUserName) $label1.AutoSize = $True $label1.DataBindings.DefaultDataSourceUpdateMode = 0 $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 6 $System_Drawing_Point.Y = 19 $label1.Location = $System_Drawing_Point $label1.Name = "label1" $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Height = 13 $System_Drawing_Size.Width = 63 $label1.Size = $System_Drawing_Size $label1.TabIndex = 21 $label1.Text = "User Name:" $groupBox1.Controls.Add($label1) #endregion Generated Form Code #Save the initial state of the form $InitialFormWindowState = $form1.WindowState #Init the OnLoad event to correct the initial state of the form $form1.add_Load($OnLoadForm_StateCorrection) #Show the Form $form1.ShowDialog()| Out-Null } #End Function #Call the Function GenerateForm
1. After you run the script in a Windows PowerShell console, you will get a window which title is “Modify Office 365 Mailbox Permission (Disconnected)”.

2. Fill in each text box with proper values. Choose an access right from the combo box. Choose an operation you want to run. Then click Run.

Note:
User Name and Password will be used to establish a connection from local Windows PowerShell to Office 365.
Mailbox Filter will be used to find all the mailboxes which you want to modify permission.
User(Alias) will be the the user mailbox alias that the permissions are being added or removed to on the other mailbox.
Access Rights specifies rights needed to perform the operation.
Allow or Deny specifies whether to allow or deny permissions to the user on the mailbox.
By default, -Whatif is checked to protect the mailbox permission from misconfiguration. If the command is right, you need to uncheck -Whatif mannually and click Run again.
If one of the textboxes is empty, you will get an error message.

3. After you click the Run button, Windows PowerShell will try to establish a connection to Office 365. After the connection is established, this script will try to modify permission of the mailboxes which are filtered out by specified mailbox filter.

4. After the operation is completed, Mailbox Filter, User (Alias) and Access Rights will be reset to blank. Meanwhile, the text of window title will be changed to “Modify Office 365 Mailbox Permission (Connected)”. This indicates that Windows PowerShell has established a connection to Office 365. The session state will be kept even the window is closed.
If you run the script in the same Windows PowerShell console again, User Name and Password will be grayed out. This behavior is by design for avoiding establishing multiple sessions.

Technical Resources:
Use Windows PowerShell in Exchange Online
http://help.outlook.com/en-us/140/cc546278.aspx
Reference to Available PowerShell Cmdlets in Exchange Online
http://help.outlook.com/en-us/140/dd575549.aspx
Add-MailboxPermission
http://technet.microsoft.com/en-us/library/bb124097.aspx
Remove-MailboxPermission
http://technet.microsoft.com/en-us/library/bb125153.aspx