# Running Powershell comand against all user mailboxes

**URL:** <https://forums.powershell.org/t/running-powershell-comand-against-all-user-mailboxes/6001>\
**Category:** PowerShell Help\
**Created:** [March 14, 2016, 8:22pm UTC](https://forums.powershell.org/t/running-powershell-comand-against-all-user-mailboxes/6001 "2016-03-14T20:22:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jules-wale](https://avatars.discourse-cdn.com/v4/letter/j/59ef9b/32.png) [@jules-wale](https://forums.powershell.org/u/jules-wale)\
**Post date:** [March 14, 2016, 8:22pm UTC](https://forums.powershell.org/t/running-powershell-comand-against-all-user-mailboxes/6001/1 "2016-03-14T20:22:50Z")

</div>

Hi

I am doing a migration for DomainA to DomainB. Currently All user mail mailbox are in DomainA on a Exchange 2010 Server.

We are mirgrating all the users to DomainB.

Is there a way to run the following comands against all users mailboxes in DomainA based on the user name of each user?

The comands are

Add-MailboxPermission “User Name1” -User “[DomainB.com](http://DomainB.com)\UName1” -AccessRights FullAccess  
Add-ADPermission “User Name1” -User “[DomainB.com](http://DomainB.com)\UName1” -Extendedrights “Send As”

Thanks

Jules

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [March 16, 2016, 1:46am UTC](https://forums.powershell.org/t/running-powershell-comand-against-all-user-mailboxes/6001/2 "2016-03-16T01:46:07Z")

</div>

Sure - you’d need to get all the mailboxes, then numerate them (ForEach), and then run the command against each, in turn.

---

<div class="post-metadata">

**Author:** ![jules-wale](https://avatars.discourse-cdn.com/v4/letter/j/59ef9b/32.png) [@jules-wale](https://forums.powershell.org/u/jules-wale)\
**Post date:** [March 16, 2016, 2:47am UTC](https://forums.powershell.org/t/running-powershell-comand-against-all-user-mailboxes/6001/3 "2016-03-16T02:47:55Z")

</div>

Thank you for that

Do you have an example?

Thanks

Jules

---

<div class="post-metadata">

**Author:** ![jules-wale](https://avatars.discourse-cdn.com/v4/letter/j/59ef9b/32.png) [@jules-wale](https://forums.powershell.org/u/jules-wale)\
**Post date:** [March 16, 2016, 3:28am UTC](https://forums.powershell.org/t/running-powershell-comand-against-all-user-mailboxes/6001/4 "2016-03-16T03:28:24Z")

</div>

I have tried this but get an error file not found

$SearchBase=“OU=UK,OU=Company,DC=Domain,DC=local” #Definition of OU containing groups I want to change  
$searchScope=1 #Depends how deep you want to search : 0 – means Base,1 – means one level, 2 – means subtree  
$CurrentDate = Get-Date  
$CurrentDate = $CurrentDate.ToString(‘dd-MMM-yyyy\_HH-mm-ss’)  
$Dir=“C:\scripts"  
$FileName=“Exchange”  
$FileName=”$Dir$FileName-$CurrentDate.txt"

Get-ADUser -SearchBase $searchbase –searchscope $searchscope -Filter \* | Select SamAccountName | ft -HideTableHeaders \> $FileName

foreach ($i in $FileName){  
Add-MailboxPermission $i -User “[NewDomain.com](http://NewDomain.com)$1” -AccessRights FullAccess  
Add-ADPermission $i -User “[NewDomain.com](http://NewDomain.com)$1” -Extendedrights “Send As”

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [March 16, 2016, 4:31am UTC](https://forums.powershell.org/t/running-powershell-comand-against-all-user-mailboxes/6001/5 "2016-03-16T04:31:21Z")

</div>

The problem is that you’re formatting the results, using FT. That renders something for the screen, not something you can then use elsewhere.

```
$FileName = Get-ADUser -SearchBase $searchbase –searchscope $searchscope -Filter * | Select SamAccountName
```

Would put a list of objects into $FileName, which each contained a samAccountName property.

In this:

```
foreach ($i in $FileName){
```

You would want to reference $i.sameAccountName, not just $i.

I also notice in your loop that:

```
Add-MailboxPermission $i -User "NewDomain.com\$1" -AccessRights FullAccess
Add-ADPermission $i -User "NewDomain.com\$1" -Extendedrights "Send As"
```

You didn’t close the } for the loop. Also, you’re referencing $1, not $i. You’d probably want $($i.samAccountName) if the goal is to insert the samAccountName. Otherwise, I’m not entirely sure what $1 is meant to refer to, as I don’t see the variable defined elsewhere in your code.

It looks like you’ve got some experience with batch files, and you’re running into some extremely common “gotchas” in PowerShell. Consider reviewing our “Gotchas” list (on the Resources menu), and consider working through “Learn PowerShell in a Month of Lunches.” There’s a certain amount of background information that would make this go a lot quicker for you.

---

<div class="post-metadata">

**Author:** ![jules-wale](https://avatars.discourse-cdn.com/v4/letter/j/59ef9b/32.png) [@jules-wale](https://forums.powershell.org/u/jules-wale)\
**Post date:** [March 17, 2016, 1:03am UTC](https://forums.powershell.org/t/running-powershell-comand-against-all-user-mailboxes/6001/6 "2016-03-17T01:03:24Z")

</div>

Thank you for your advise, I will look at that.

I have made some changes

`
$SearchBase="OU=UK,OU=OwenMumford,DC=woodstock,DC=local" #Definition of OU containing groups I want to change
$searchScope=1 #Depends how deep you want to search : 0 – means Base,1 – means one level, 2 – means subtree
$CurrentDate = Get-Date
$CurrentDate = $CurrentDate.ToString('dd-MMM-yyyy_HH-mm-ss')
$Dir="C:\scripts\"
$FileName="Exchange"
$FileName="$Dir$FileName-$CurrentDate.txt"
$FileName = Get-ADUser -SearchBase $searchbase –searchscope $searchscope -Filter * | Select SamAccountName
foreach ($i in $FileName){
Add-MailboxPermission $i -User “OwenMumford.com$($i.samAccountName)” -AccessRights FullAccess
Add-ADPermission $i -User “OwenMumford.com$($i.samAccountName)” -Extendedrights “Send As”
}

and I get the error:
Cannot bind argument to parameter ‘Identity’ because it is null.
+ CategoryInfo : InvalidData: (🙂 [Add-MailboxPermission], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Add-MailboxPermission
Cannot bind argument to parameter ‘Identity’ because it is null.
+ CategoryInfo : InvalidData: (🙂 [Add-ADPermission], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Add-ADPermission
THanks
Jules`

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [March 17, 2016, 1:08am UTC](https://forums.powershell.org/t/running-powershell-comand-against-all-user-mailboxes/6001/7 "2016-03-17T01:08:20Z")

</div>

So, again: $i contains a user object. You need to reference $i.samAccountName. You should also explicitly use parameter names on Add-MailboxPermission and Add-ADPermission. Right now, you’re passing $i, not $i.samAccountName, and you’re passing it positionally, which is forcing the cmdlet to try and figure out what you mean. And it’s guessing wrong, which is why it isn’t working.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:42pm UTC](https://forums.powershell.org/t/running-powershell-comand-against-all-user-mailboxes/6001/8 "2024-05-16T20:42:37Z")

</div>


