Question about Best Practice - IfElse or Switch

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?

 

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…

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.

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'}
}

@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'}
}

Thanks to both of you, @Iain and @Rob