Remove-Mailbox Permissions in bulk - powershell

I've been asked to remove a user from mailbox full access permissions. I need this one for every mailbox in the company or specific users (around 180).
So that's what I did so far:
Remove-MailboxPermission -Identity "John Duo" -User "Nik Biessen" -AccessRights FullAccess -InheritanceType All
This removes the delegation rights from one user. I need the same thing just for all mailboxes or a list of them to be executed in one script.
Thanks in advance

You can easily do it using a foreach loop.
Get-Mailbox -ResultSize Unlimited |Foreach {Remove-MailboxPermission -Identity $_.samaccountname -User "Nik Biessen" -AccessRights FullAccess -InheritanceType All}
Hope Nik Biessen is the user who got access, which needs to be removed.
Please note that the csv file needs a header as ID in the first line.
Note - Code is not tested. Please test it before running it in a production environment.

Related

Script to copy Exchange Distribution Groups from one user to another

I am hoping to get some help with a script to copy Exchange group permissions from one user to another. I currently have a script that works to copy mailbox permissions from one user to another but would like to expand it so that it can do Distribution Groups as well.
Connect-ExchangeOnline
$FUser = Read-Host "Enter the email address of the user you want to copy mailbox permissions from"
$TUser = Read-Host "Enter the email address of the user you want to set mailbox permissions for"
$GPerm = Get-Mailbox | Get-MailboxPermission -User $FUser
$GPerm | ForEach-Object { $_
Add-MailboxPermission -Identity $_.Identity -AccessRights FullAccess -InheritanceType All -User $TUser
Add-RecipientPermission -Identity $_.Identity -AccessRights SendAs -Confirm:$false -Trustee $TUser
}
While looking online I found a similar question online asked by someone else but their question was about coping the DL members from one to another DL.
Get-DistributionGroupMember -Identity "A" | % {add-distributiongroupmember -Identity "B" -Member $_.Name}
Additonally I was able to find a script working to remove the permissions for DLs. But didn't work if I changed the parts from remove to add. But the script isn't for what I am looking for as removing permissions and copying are two different things.
Thanks,
daaqis

Exchange online - Copy mailbox permissions from one user to another

I'm Trying to find a way to copy a users mailbox permissions to another user, I can output the data I need in PS just can't find a way to then apply those permissions to the new user.
I'm not amazing with PS so please bare with me :)
Get-Mailbox -RecipientTypeDetails UserMailBox,SharedMailbox | Get-MailboxPermission -User
which then outputs the users permissions but I would like to be able to then add those permissions to my new user in the same script.
hope this helps:
$FromUser = Read-Host "Enter the email address of the user you want to copy mailbox permissions from"
$ToUser = Read-Host "Enter the email address of the user you want to set mailbox permissions for"
$Perm = Get-Mailbox | Get-MailboxPermission -User $FromUser
$Perm | ForEach-Object { $_
Add-MailboxPermission -Identity $_.Identity -AccessRights FullAccess -InheritanceType All -AutoMapping:$true -User $ToUser
Add-RecipientPermission -Identity $_.Identity -AccessRights SendAs -Confirm:$false -Trustee $oTUser
}
This will automatically find the permissions from User1 to User2. You can change the Parameters to whatever you want to put based on Microsofts allowed commands.
https://learn.microsoft.com/en-us/powershell/module/exchange/add-mailboxpermission?view=exchange-ps
https://learn.microsoft.com/en-us/powershell/module/exchange/add-recipientpermission?view=exchange-ps

New-mailbox script, with zipcode and P.O. Box values added to mailbox user account. possible?

I am using the following powershell code for creating new mailboxes in my organization.
$users = Import-CSV C:\mailboxes.csv
$users| foreach {
$Password = convertto-securestring $_.password -asplaintext -force
new-mailbox -name $_.name -alias $_.alias -FirstName $_.Firstname -LastName $_.Lastname -userPrincipalName $_.userPrincipalName -PrimarySmtpAddress $_.PrimarySmtpAddress -Database $_.database -RetentionPolicy "b3a83dc4-e471-4d05-b357-25535aa027af" -OrganizationalUnit $_.OrganizationalUnit -Password $Password –ResetPasswordOnNextLogon:$false
}
Is there a way to insert a static text/value to this "zip code" and "po box" boxes, on the new active directory user, created along with this mailboxes?
for example , zip code should contain: "0101010101" and P.O Box should contain "000"
Your assistance is most appreciated
One option is to use Set-ADUser from the ActiveDirectory module. At the beginning of your script (before any loops), you can run the following if you have the module available to your current session.
Import-Module ActiveDirectory
After your New-Mailbox command, you can add the Set-ADUser command:
Set-ADUser -Filter "UserPrincipalName -eq '$($_.userprincipalname)'" -PostalCode "01010101" -POBox "000"
Sometimes AD replication can cause inconsistencies with multiple commands against AD objects. To get around that, you would typically use the -Server parameter to consistently target a domain controller that will see all of your read and write operations. The alternative (a slower one) is to run the AD user modifications after all of the mailboxes have been created and data has replicated to the AD Site you would be targeting.
AdminOfThings - Thanks for your reply.
So tell me,
Considering your last comment about the AD User modification conflict that i might occur,
i`m thinking some sort of "time delay" code might resolve such issues.
would it be logical to add something like "Start-Sleep" command to add a delay between
the "new-mailbox" and "Set-ADUser" commands as you suggested?
if so can you...write down how my script should like exactly, adding all things together please?
Thanks.

Remove full access permissions of all disabled users on shared mailboxes with exchange management shell

I’m looking for a powershell exchange script to remove Full access permissions of all disabled users on all shared mailboxes in a specific OU.
This is what I got so far
Remove-MailboxPermission -Identity Sharedmailbox -AccessRights Fullaccess -InheritanceType all -user DisabledUser -Confirm:$false | where {$_.UseraccountControl -like "*accountdisabled*"}
Its seems to work but I’m not sure about the last piece of het script if it will check for “accountdisabled”
Then I created a variable so it will check only one specific OU
$ou = Get-ADUser -SearchBase "OU=Functional Mailboxes,OU=Generalaccounts,DC=DOMAIN,DC=COM" -Filter * foreach ($user in $ou)
Remove-MailboxPermission -Identity "$ou" -AccessRights Fullaccess -InheritanceType all -Confirm:$false | where {$_.UseraccountControl -like "*accountdisabled*"}
The script is checking the right OU but I'm still looking for the last part where it will automatically remove full access permissions of the disabled users ONLY.
Can someone show me the way?
Instead of trying to screen for disabled users after removing the mailbox permissions (which is what your Remove-MailboxPermission ... | Where-Object ... appears to be intended to do - except that the way you wrote it, it's only checking for disabled state after removing the permissions), try selecting for the disabled accounts first, then passing only the disabled accounts to Remove-MailboxPermission:
Get-ADUser -SearchBase ... -filter {Enabled -eq $false} | Remove-Mailbox ...
(replacing ... with the appropriate SearchBase or parameters for Remove-Mailbox, using $_ for the identity of the ADUser whose mailbox permissions you're removing.)

Exchange 2010: How can I check what permissions a user/mailbox has towards other mailboxes?

I know how to check who has Full Access or Send As permissions on a specific mailbox, but how can I check if a specific user has Full Access or Send As permissions on any mailbox?
By running Get-MailboxPermission cmdlet you can check which user/mailbox has what type of permissions to access other mailboxes in Exchange.
Check this helpful. And I'm sure it is what you was looking for.
http://exchangeserverpro.com/list-users-access-exchange-mailboxes/
And I also check this helpful
Get-Mailboxpermission for list of Mailboxes
This can be achieved by user the following powershell command:
Get-Mailbox | Get-MailboxPermission -User 'username'
The problem i run into that this doesn't include 'Security Groups' with mailbox permissions that a user might be member of.
If anyone knows how to solve this i would highly appreciate a reply.
Actually John Dane's answer is correct...it works for groups as well. The -User parameter accepts DistinguishedName or SamAccountName...both of which AD Security Groups have.
So just pass it the SamAccountName (or 'username') of your group and your golden. I used this to find out which mailbox an old group we were thinking about retiring had permissions to. I added a "| ft -autosize" to see the full identity field of the mailbox in the default output.
Get-Mailbox | Get-MailboxPermission -User 'SamAccountName'| ft -autosize
or just select the identity and access rights if that's all you need.
Get-Mailbox | Get-MailboxPermission -User 'SamAccountName'| select Identity,AccessRights | ft -autosize
With the following Command you don't have any missing entries:
Get-Mailbox -resultsize unlimited | Get-MailboxPermission | Where {(!$_.isinherited) -and ($_.user.SecurityIdentifier -ne "S-1-5-10") -and ($_.accessrights -contains "fullaccess") } | Select Identity,User | Export-Csv -Path "c:\temp\testmailboxpermissions.csv"