# Switch and IF, can it be done smarter?

**URL:** <https://forums.powershell.org/t/switch-and-if-can-it-be-done-smarter/19173>\
**Category:** PowerShell Help\
**Created:** [April 25, 2022, 12:39pm UTC](https://forums.powershell.org/t/switch-and-if-can-it-be-done-smarter/19173 "2022-04-25T12:39:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Sylvest](https://avatars.discourse-cdn.com/v4/letter/d/ecccb3/32.png) [@David\_Sylvest](https://forums.powershell.org/u/David_Sylvest)\
**Post date:** [April 25, 2022, 12:39pm UTC](https://forums.powershell.org/t/switch-and-if-can-it-be-done-smarter/19173/1 "2022-04-25T12:39:43Z")

</div>

Hi.

I am working on a AD script to create a user, i am at a point where i am addding exchange groups. Below is just an example of the challenge. I got multiple countries, and based on different factors etc, department then they need to be added to certerin exchange groups. I am currently working with Switch and then IF, but i end up doing a lot of code copy-pasting.

Is there a smarter way to write such logic?

```auto

switch ($Country) {
    US {
            add user to "A" exchange groups
            If (user department = sales then add user to "B" exchange groups
            if (user department = Accounting then add user to "C" exchange groups 
    }

    DK { 
            add user to "D" exchange groups
            If (user department = sales then add user to "E" exchange groups
            if (user department = Accounting then add user to "F" exchange groups 
    }
    
    DE { 
         and so on ...
    }

    GB { 
          and on...   
    }

    NL { 
        .....
    }

    IT { 
        
    }

    FR { 
        
    }

    AT { 
       
    }

    CH { 
        
    }

    PH { 

    }

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [April 25, 2022, 12:49pm UTC](https://forums.powershell.org/t/switch-and-if-can-it-be-done-smarter/19173/2 "2022-04-25T12:49:19Z")

</div>

Are the `"Z exchange groups"` (or “X” or “Y”) the same for the `"US"` and `"DK"` and so on? If Yes why make a difference then? If no - what do you meant with …

> [@David\_Sylvest](#):
>
> but they ends up being alot of copy-phaste.

---

<div class="post-metadata">

**Author:** ![David\_Sylvest](https://avatars.discourse-cdn.com/v4/letter/d/ecccb3/32.png) [@David\_Sylvest](https://forums.powershell.org/u/David_Sylvest)\
**Post date:** [April 25, 2022, 1:02pm UTC](https://forums.powershell.org/t/switch-and-if-can-it-be-done-smarter/19173/3 "2022-04-25T13:02:24Z")

</div>

Hi Olaf.

I can see that is unclear.  
The Z, Y, X groups are different for each country.

I meant to write: With my current code logic, then i end up doing a lot of copy-pasting.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [April 25, 2022, 1:14pm UTC](https://forums.powershell.org/t/switch-and-if-can-it-be-done-smarter/19173/4 "2022-04-25T13:14:57Z")

</div>

> [@David\_Sylvest](#):
>
> The Z, Y, X groups are different for each country.

OK, so you have to have a kind of decision tree. And if the groups in the different countries are different I don’t see another way than to have them all individually represented in this decision tree. 🤷🏽‍♂️

> [@David\_Sylvest](#):
>
> I meant to write: With my current code logic, then i end up doing a lot of copy-pasting.

It sounds like an onboarding process. Usually you put new employees of certain departments in dedicated groups and the rest should come from this group memberships. But the initial _treatment_ may be - depending on the structure of your company - cumbersome. 🤷🏽‍♂️

---

<div class="post-metadata">

**Author:** ![David\_Sylvest](https://avatars.discourse-cdn.com/v4/letter/d/ecccb3/32.png) [@David\_Sylvest](https://forums.powershell.org/u/David_Sylvest)\
**Post date:** [April 25, 2022, 1:34pm UTC](https://forums.powershell.org/t/switch-and-if-can-it-be-done-smarter/19173/5 "2022-04-25T13:34:47Z")

</div>

Thanks for your feedback Olaf, that is much appriciated. 🙂

---

<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:18pm UTC](https://forums.powershell.org/t/switch-and-if-can-it-be-done-smarter/19173/6 "2024-05-16T20:18:03Z")

</div>


