# Question about Best Practice - IfElse or Switch

**URL:** <https://forums.powershell.org/t/question-about-best-practice-ifelse-or-switch/14710>\
**Category:** PowerShell Help\
**Created:** [July 27, 2020, 9:09am UTC](https://forums.powershell.org/t/question-about-best-practice-ifelse-or-switch/14710 "2020-07-27T09:09:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![alombardi](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@alombardi](https://forums.powershell.org/u/alombardi)\
**Post date:** [July 27, 2020, 9:09am UTC](https://forums.powershell.org/t/question-about-best-practice-ifelse-or-switch/14710/1 "2020-07-27T09:09:02Z")

</div>

Wanting to basically have “options” within a script. If a user is a member of a specific OU (based on their distinguished name) do this task. If they are a member of another OU, do a separate (but nearly identical thing).

I’ve seen some mention using an ElseIf combination but I’ve seen elsewhere that people suggest using Switches. I’m honestly not super familiar with loops and even less so with switches. Anyone have any input/suggestions?

&nbsp;

```
Basic idea:
If OU = ABC then grab attribute value and place it in ABCtxt document
else
If OU = DEF then grab attribute value and place it in DEFtxt document
else
If OU = JKL then grab attribute value and place it in JKLtxt document
```

etc…

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [July 27, 2020, 9:23am UTC](https://forums.powershell.org/t/question-about-best-practice-ifelse-or-switch/14710/2 "2020-07-27T09:23:35Z")

</div>

If you are comparing the same variable, then you should use switch:

```
$ous = 'CN=Marketing,DC=child,DC=company,DC=com',
       'CN=Sales,DC=child,DC=company,DC=com',
       'CN=Foo,DC=child,DC=company,DC=com'

foreach ($ou in $ous) {

    switch -wildcard ($ou) {
        '*Sales*' {'Doing stuff for Sales'}
        '*Marketing*' {'Doing stuff for Marketing'}
        default {'Doing default stuff because the OU {0} is not defined above' -f $_}
    }
}
```

**Output:**

```
Doing stuff for Marketing
Doing stuff for Sales
Doing default stuff because the OU CN=Foo,DC=child,DC=company,DC=com is not defined above
```

Personally rarely use ElseIf logic in Powershell.

---

<div class="post-metadata">

**Author:** ![firmbyte](https://avatars.discourse-cdn.com/v4/letter/f/e68b1a/32.png) [@firmbyte](https://forums.powershell.org/u/firmbyte)\
**Post date:** [July 27, 2020, 9:24am UTC](https://forums.powershell.org/t/question-about-best-practice-ifelse-or-switch/14710/3 "2020-07-27T09:24:29Z")

</div>

I like to use a Switch for this type of thing

```
$ou = 'z4r'

switch ($ou)

{

 {$\_ -eq 'xyz'} {$Text = 'abc'}

 {$\_ -eq '123'} {$Text = 'jkf'}

 {$\_ -eq 'abc'} {$Text = 'lmp'}

 {$\_ -eq 'z4r'} {$Text = 'rst'}

}
```

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [July 27, 2020, 9:32am UTC](https://forums.powershell.org/t/question-about-best-practice-ifelse-or-switch/14710/4 "2020-07-27T09:32:14Z")

</div>

@Iain

The script block is unnecessary as -eq is the default:

```
$ou = 'z4r'

switch ($ou)
{
    'xyz' {$Text = 'abc'}
    '123' {$Text = 'jkf'}
    'abc' {$Text = 'lmp'}
    'z4r' {$Text = 'rst'}
}
```

---

<div class="post-metadata">

**Author:** ![alombardi](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@alombardi](https://forums.powershell.org/u/alombardi)\
**Post date:** [July 27, 2020, 9:46am UTC](https://forums.powershell.org/t/question-about-best-practice-ifelse-or-switch/14710/5 "2020-07-27T09:46:35Z")

</div>

Thanks to both of you, @Iain and @Rob

---

<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/question-about-best-practice-ifelse-or-switch/14710/6 "2024-05-16T20:29:11Z")

</div>


