# Looping through a script with two variables

**URL:** <https://forums.powershell.org/t/looping-through-a-script-with-two-variables/15110>\
**Category:** PowerShell Help\
**Created:** [October 19, 2020, 1:54pm UTC](https://forums.powershell.org/t/looping-through-a-script-with-two-variables/15110 "2020-10-19T13:54:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mpreziosi](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@mpreziosi](https://forums.powershell.org/u/mpreziosi)\
**Post date:** [October 19, 2020, 1:54pm UTC](https://forums.powershell.org/t/looping-through-a-script-with-two-variables/15110/1 "2020-10-19T13:54:01Z")

</div>

I need to block all student access to all faculty Outlook calendars. I can use the below script but I need to pass two variables, a list of 800 faculty and a list of 40,000 students. How do I make sure the script loops through every user for every mailbox?

add-mailboxfolderpermission -identity “$faculty`:\calendar” -user $students -accessrights none

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [October 19, 2020, 8:01pm UTC](https://forums.powershell.org/t/looping-through-a-script-with-two-variables/15110/2 "2020-10-19T20:01:00Z")

</div>

You can use common looping statements. The most commonly used one in PowerShell is the Foreach keyword. Below documentation will help you to do it.

> **[about Foreach - PowerShell](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_foreach?view=powershell-7)**
>
> Describes a language command you can use to traverse all the items in a collection of items.

---

<div class="post-metadata">

**Author:** ![mpreziosi](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@mpreziosi](https://forums.powershell.org/u/mpreziosi)\
**Post date:** [October 20, 2020, 2:53am UTC](https://forums.powershell.org/t/looping-through-a-script-with-two-variables/15110/3 "2020-10-20T02:53:31Z")

</div>

Yeah, i am having trouble figuring out how to loop through the two variables. Thanks for the link; i am still confused about this.

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [October 20, 2020, 3:07am UTC](https://forums.powershell.org/t/looping-through-a-script-with-two-variables/15110/4 "2020-10-20T03:07:32Z")

</div>

One way of doing it is to nest the foreach loops:

```
$faculties = 'Maths','Music','Law','Art'
$students = 'Buffy','Willow','Xander','Cordelia'

foreach ($faculty in $faculties) {

    foreach ($student in $students) {

        Write-Output "$faculty - $student"
    
    }

}
```

I wonder if you do need to loop through all the students though. Surely, the best way to do this is to have all students in a group, and add the group to all mailboxes. You really don’t want 40,000 objects in each mailbox ACL.

---

<div class="post-metadata">

**Author:** ![mpreziosi](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@mpreziosi](https://forums.powershell.org/u/mpreziosi)\
**Post date:** [October 20, 2020, 3:12am UTC](https://forums.powershell.org/t/looping-through-a-script-with-two-variables/15110/5 "2020-10-20T03:12:49Z")

</div>

Oh, what a great idea! Of course, I didn’t even think of using a group. Thank you so much for the example also! Makes perfect sense when I see it now.

---

<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:29pm UTC](https://forums.powershell.org/t/looping-through-a-script-with-two-variables/15110/6 "2024-05-16T20:29:12Z")

</div>

Closed on May 16, 2024, 8:29pm UTC


