# Advice on a script

**URL:** <https://forums.powershell.org/t/advice-on-a-script/14855>\
**Category:** PowerShell Help\
**Created:** [August 25, 2020, 12:45am UTC](https://forums.powershell.org/t/advice-on-a-script/14855 "2020-08-25T00:45:07Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![chm0d](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/chm0d/32/34_2.png) [@chm0d](https://forums.powershell.org/u/chm0d)\
**Post date:** [August 25, 2020, 12:45am UTC](https://forums.powershell.org/t/advice-on-a-script/14855/1 "2020-08-25T00:45:07Z")

</div>

Good morning fellow Powershellers! Hope this finds everyone well. I am currently in the process of creating a New-ADUser script and I would like advice on how to accomplish something. I work for a school district and we have about 42 different cost centers each having a number like 9020, 1251, 0451, etc. I know I could have a variable for each one and do a ton of if statements to put the user in the correct OU, but I was wondering if there is another way that I am not aware of? So basically I have 42 different OU’s these new users can go in depending on what cost center they will be working. Could anyone give me another way to accomplish this other than a ton of variables? Thank you in advanced. Hope everyone has a good day!

Rich

---

<div class="post-metadata">

**Author:** ![laage](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/laage/32/43_2.png) [@laage](https://forums.powershell.org/u/laage)\
**Post date:** [August 25, 2020, 1:32am UTC](https://forums.powershell.org/t/advice-on-a-script/14855/2 "2020-08-25T01:32:59Z")

</div>

You might want to look into the Switch statement for this.

You don’t say exactly where the info comes into play, so I’ll just do a quick pseudocode example:

```
switch ($costcenter) {

9020 {

```
add-user to OU '9020'

break

```

}

1251 {

```
add-user to OU '1251'

break

```

}

… snip …

default {

```
Write-Error 'No matching OU found'

break

```

}

}
```

Hope this can help you.

---

<div class="post-metadata">

**Author:** ![chm0d](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/chm0d/32/34_2.png) [@chm0d](https://forums.powershell.org/u/chm0d)\
**Post date:** [August 25, 2020, 3:27am UTC](https://forums.powershell.org/t/advice-on-a-script/14855/3 "2020-08-25T03:27:56Z")

</div>

[quote quote=252314]You might want to look into the Switch statement for this.

You don’t say exactly where the info comes into play, so I’ll just do a quick pseudocode example:

PowerShell

\<textarea class="urvanov-syntax-highlighter-plain print-no" style="tab-size: 4; font-size: 14px !important; line-height: 18px !important; z-index: 0; opacity: 0;" readonly="readonly" data-settings="dblclick"\>switch ($costcenter) {

9020 {

```
add-user to OU '9020'

break

```

}

1251 {

```
add-user to OU '1251'

break

```

}

… snip …

default {

```
Write-Error 'No matching OU found'

break

```

}

}\</textarea\>

| 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

 | 

switch ($costcenter) {

9020 {

add-user to OU '9020'

break

}

1251 {

add-user to OU '1251'

break

}

... snip ...

default {

Write-Error 'No matching OU found'

break

}

}

 |

Hope this can help you.

[/quote]  
Thank you for the reply. I can try the switch statement. Have a great day!

&nbsp;

Rich

---

<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:** [August 25, 2020, 5:21am UTC](https://forums.powershell.org/t/advice-on-a-script/14855/4 "2020-08-25T05:21:29Z")

</div>

Just build the path with a variable, no need for switch unless you have multiple costcenters that go into OU’s or soemthing like that.

```
$costcenter = 1234

$ouPath = 'OU={0},OU=Districts,DC=myschool,DC=edu' -f $costcenter

'Adding user to OU: {0}' -f $ouPath
```

An example of processing would look like:

```
$costcenters = 1234,4524,2223,4532

foreach ($costcenter in $costcenters) {
    $ouPath = 'OU={0},OU=Districts,DC=myschool,DC=edu' -f $costcenter
    'Adding user to OU: {0}' -f $ouPath
}
```

Output:

```
Adding user to OU: OU=1234,OU=Districts,DC=myschool,DC=edu
Adding user to OU: OU=4524,OU=Districts,DC=myschool,DC=edu
Adding user to OU: OU=2223,OU=Districts,DC=myschool,DC=edu
Adding user to OU: OU=4532,OU=Districts,DC=myschool,DC=edu
```

---

<div class="post-metadata">

**Author:** ![chm0d](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/chm0d/32/34_2.png) [@chm0d](https://forums.powershell.org/u/chm0d)\
**Post date:** [August 25, 2020, 12:15pm UTC](https://forums.powershell.org/t/advice-on-a-script/14855/5 "2020-08-25T12:15:16Z")

</div>

[quote quote=252350]Just build the path with a variable, no need for switch unless you have multiple costcenters that go into OU’s or soemthing like that.

PowerShell

\<textarea class="urvanov-syntax-highlighter-plain print-no" style="tab-size: 4; font-size: 14px !important; line-height: 18px !important; z-index: 0; opacity: 0;" readonly="readonly" data-settings="dblclick"\>$costcenter = 1234

$ouPath = ‘OU={0},OU=Districts,DC=myschool,DC=edu’ -f $costcenter

‘Adding user to OU: {0}’ -f $ouPath\</textarea\>

| 

1

2

3

4

5

 | 

$costcenter = 1234

$ouPath = 'OU={0},OU=Districts,DC=myschool,DC=edu' -f $costcenter

'Adding user to OU: {0}' -f $ouPath

 |

An example of processing would look like:

PowerShell

\<textarea class="urvanov-syntax-highlighter-plain print-no" style="tab-size: 4; font-size: 14px !important; line-height: 18px !important; z-index: 0; opacity: 0;" readonly="readonly" data-settings="dblclick"\>$costcenters = 1234,4524,2223,4532

foreach ($costcenter in $costcenters) {  
$ouPath = ‘OU={0},OU=Districts,DC=myschool,DC=edu’ -f $costcenter  
‘Adding user to OU: {0}’ -f $ouPath  
}\</textarea\>

| 

1

2

3

4

5

6

 | 

$costcenters = 1234,4524,2223,4532

foreach ($costcenter in $costcenters) {

$ouPath = 'OU={0},OU=Districts,DC=myschool,DC=edu' -f $costcenter

'Adding user to OU: {0}' -f $ouPath

}

 |

Output:

PowerShell

\<textarea class="urvanov-syntax-highlighter-plain print-no" style="tab-size: 4; font-size: 14px !important; line-height: 18px !important; z-index: 0; opacity: 0;" readonly="readonly" data-settings="dblclick"\>Adding user to OU: OU=1234,OU=Districts,DC=myschool,DC=edu Adding user to OU: OU=4524,OU=Districts,DC=myschool,DC=edu Adding user to OU: OU=2223,OU=Districts,DC=myschool,DC=edu Adding user to OU: OU=4532,OU=Districts,DC=myschool,DC=edu\</textarea\>

| 

1

2

3

4

 | 

Adding user to OU: OU=1234,OU=Districts,DC=myschool,DC=edu

Adding user to OU: OU=4524,OU=Districts,DC=myschool,DC=edu

Adding user to OU: OU=2223,OU=Districts,DC=myschool,DC=edu

Adding user to OU: OU=4532,OU=Districts,DC=myschool,DC=edu

 |

[/quote] Hey Rob thanks again for replying. Unfortunately we have our CostCenter OU's pretty much all over the place in Active Directory. I started messing with the switch statement today and I have it working ok. I just hate messy looking code. Was hoping there was an easier way of doing this that I didn't know of. I greatly appreciate your response though. Stay safe out there!

Rich

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [August 25, 2020, 6:56pm UTC](https://forums.powershell.org/t/advice-on-a-script/14855/6 "2020-08-25T18:56:32Z")

</div>

If you don’t mind querying each time you can just look up the OU.

```
$costcenters = 1234,4524,2223,4532

$costcenters | foreach {
    $oupath = Get-ADOrganizationalUnit -Filter "name -like '$_'" | Select-Object -ExpandProperty distinguishedname
    ... add user
}
```

---

<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:** [August 25, 2020, 8:39pm UTC](https://forums.powershell.org/t/advice-on-a-script/14855/7 "2020-08-25T20:39:23Z")

</div>

Don’t you need a wildcard to use the -like filter?

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [August 26, 2020, 3:38am UTC](https://forums.powershell.org/t/advice-on-a-script/14855/8 "2020-08-26T03:38:51Z")

</div>

> [@](#):
>
> Don’t you need a wildcard to use the -like filter?

You know for several things you do, but if it matches the name completely it’s not required. I tested Get-ADGroup, Get-ADComputer, Get-ADOrganizationalUnit. However, if it’s a partial match then you definitely need it.

```
OU: SomeName

Get-ADOrg -Filter "Name -like 'name'" # won't find it

Get-ADOrg -Filter "Name -like 'somename'" # will find

Get-ADOrg -Filter "Name -like '*name'" # will find
```

---

<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:** [August 26, 2020, 3:53am UTC](https://forums.powershell.org/t/advice-on-a-script/14855/9 "2020-08-26T03:53:34Z")

</div>

> [@](#):
>
> … but if it matches the name completely it’s not required.

I haven’t had this particular case in my mind. 😉

---

<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:** [August 26, 2020, 4:45am UTC](https://forums.powershell.org/t/advice-on-a-script/14855/10 "2020-08-26T04:45:35Z")

</div>

This also assumes there is a 1:1 match for that costcenter search. If there are no matches or multiple matches, you need to have a default OU to place objects.

```
$costcenter = 1234
$oupath = Get-ADOrganizationalUnit -Filter "name -like '$costcenter'" | Select-Object -ExpandProperty distinguishedname

if (@($oupath).Count -ne 1) {
    $ouPath = 'OU=MyDefaultOU,OU=Districts,DC=myschool,DC=edu'
    'Search for costcenter {0} returned {1} matches, using default path {3}' -f $costcenter,$oupath,$oupath
}

Add-AdUser ...
```

---

<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:28pm UTC](https://forums.powershell.org/t/advice-on-a-script/14855/11 "2024-05-16T20:28:58Z")

</div>


