title: Nested ForEach statements - Exchange Powershell - bulk remove mailbox calendar permissions -
body: <p>This is my first post; I'm pretty new to powershell.</p>
  
  <p><em>At a customer request I am changing the Calendar access permissions for all user in their Exchange 365.
  I set the default to 'Reviewer' for all mailboxes using code found on <a href="https://community.spiceworks.com/how_to/135665-office-365-setting-default-calendar-permissions-to-reviewer-for-all-users" rel="nofollow noreferrer">Spiceworks</a> :</em></p>
  
  <pre><code>$users = get-mailbox
  foreach ($user in $users) {set-mailboxfolderpermission -identity $($user.alias):\calendar" -User Default -AccessRights Reviewer  -confirm:$false}
  </code></pre>
  
  <p><em>And I am able to remove all other user's permissions from a specified mailbox using script I concocted from the above:</em></p>
  
  <pre><code> $users = get-mailbox
      foreach ($user in $users) {Write-host -foregroundcolor green "Removing permission for $($user.alias)..."
      remove-mailboxfolderpermission -identity username:\calendar -user $user.alias -confirm:$false}
  </code></pre>
  
  <p>I was hoping to workout how to remove all explicit permissions from all mailboxes in one command for any future use; I think it will involve nested foreach statements but I'm struggling to get the double loop right. </p>
  
  <p>It needs to run something like this:</p>
  
  <p>$users = get-mailbox</p>
  
  <p>foreach $user {remove-mailboxfolderpermission -identity <strong>user1</strong>.alias:\calendar -user <strong>$user1</strong>.alias}</p>
  
  <p>foreach $user {remove-mailboxfolderpermission -identity <strong>user1</strong>.alias:\calendar -user <strong>$user2</strong>.alias}</p>
  
  <p>foreach $user {remove-mailboxfolderpermission -identity <strong>user1</strong>.alias:\calendar -user <strong>$user3</strong>.alias} </p>
  
  <p>foreach $user {remove-mailboxfolderpermission -identity <strong>user1</strong>.alias:\calendar -user <strong>$user4</strong>.alias} </p>
  
  <p>foreach $user {remove-mailboxfolderpermission -identity <strong>user2</strong>.alias:\calendar -user <strong>$user1</strong>.alias} </p>
  
  <p>foreach $user {remove-mailboxfolderpermission -identity <strong>user2</strong>.alias:\calendar -user <strong>$user2</strong>.alias} </p>
  
  <p>foreach $user {remove-mailboxfolderpermission -identity <strong>user2</strong>.alias:\calendar -user <strong>$user3</strong>.alias} </p>
  
  <p>foreach $user {remove-mailboxfolderpermission -identity <strong>user2</strong>.alias:\calendar -user <strong>$user4</strong>.alias} </p>
  
  <p>foreach $user {remove-mailboxfolderpermission -identity <strong>user3</strong>.alias:\calendar -user <strong>$user1</strong>.alias}</p>
  
  <p>Etc</p>
  
  <p>But it's pulling the user.alias from the same array so in my experiments it either runs once (the same entry for -identity and -user) or it fails with a error or runs forever. </p>
  
  <p>Any assistance, tips or general advice regarding nested ForEach pulling from the same source would be helpful.</p>