# Stripping sections of an OU out to populate AD fields

**URL:** <https://forums.powershell.org/t/stripping-sections-of-an-ou-out-to-populate-ad-fields/20879>\
**Category:** PowerShell Help\
**Created:** [November 9, 2022, 4:10pm UTC](https://forums.powershell.org/t/stripping-sections-of-an-ou-out-to-populate-ad-fields/20879 "2022-11-09T16:10:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![SSHero](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/sshero/32/4058_2.png) [@SSHero](https://forums.powershell.org/u/SSHero)\
**Post date:** [November 9, 2022, 4:10pm UTC](https://forums.powershell.org/t/stripping-sections-of-an-ou-out-to-populate-ad-fields/20879/1 "2022-11-09T16:10:59Z")

</div>

Hi I am wanting to add a section to a script to use a certain segment of the users OU to populate fields in AD.

For example I know I can do Get-aduser $username -property distinguishedName to get the OU. I then want to know how I can strip out the below section for example:

OU=JUST THIS TEXT, OU=TEST, OU=TEST

Hope it makes sense

---

<div class="post-metadata">

**Author:** ![mawazo](https://avatars.discourse-cdn.com/v4/letter/m/48db29/32.png) [@mawazo](https://forums.powershell.org/u/mawazo)\
**Post date:** [November 9, 2022, 4:33pm UTC](https://forums.powershell.org/t/stripping-sections-of-an-ou-out-to-populate-ad-fields/20879/2 "2022-11-09T16:33:42Z")

</div>

hey

try below

```auto
$userObj = Get-aduser -Identity user1 

# grab the first OU, learnt this from Olaf in this forum :) 
$OU = ($userObj.DistinguishedName -split ',')[1]

#remove 'OU=' 
$OU -replace 'OU=',''

```

search the forum, should be a few more examples

---

<div class="post-metadata">

**Author:** ![SSHero](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/sshero/32/4058_2.png) [@SSHero](https://forums.powershell.org/u/SSHero)\
**Post date:** [November 9, 2022, 4:38pm UTC](https://forums.powershell.org/t/stripping-sections-of-an-ou-out-to-populate-ad-fields/20879/3 "2022-11-09T16:38:32Z")

</div>

This looks good but as our OU’s aren’t always the same length I need to work from the other end is that possible?

---

<div class="post-metadata">

**Author:** ![SSHero](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/sshero/32/4058_2.png) [@SSHero](https://forums.powershell.org/u/SSHero)\
**Post date:** [November 9, 2022, 4:40pm UTC](https://forums.powershell.org/t/stripping-sections-of-an-ou-out-to-populate-ad-fields/20879/4 "2022-11-09T16:40:25Z")

</div>

Or can I retrieve the OU to only a certain length to normalize it?

---

<div class="post-metadata">

**Author:** ![mawazo](https://avatars.discourse-cdn.com/v4/letter/m/48db29/32.png) [@mawazo](https://forums.powershell.org/u/mawazo)\
**Post date:** [November 9, 2022, 5:04pm UTC](https://forums.powershell.org/t/stripping-sections-of-an-ou-out-to-populate-ad-fields/20879/5 "2022-11-09T17:04:16Z")

</div>

i assume you want the OU where the user is sitting in. don’t think the length matters.

```auto
# distinguished property in ad example
$DistinguishedName = 'CN=mawazo,OU=Location with very long name,OU=test users,OU=all users,DC=potato,DC=com'

# change string to array by splitting it using comma
$array =
$DistinguishedName -split ','

#you get this 

CN=mawazo
OU=Location with very long name
OU=test users 
OU=all users
DC=potato
DC=com

# grab the first ou which is second from the list
$array[1]

#you get this
OU=Location with very long name

# to get the last ou
$array[-3]

```

anyway as always the people here are helpful so someone will jump in to assist.

---

<div class="post-metadata">

**Author:** ![Matt](https://avatars.discourse-cdn.com/v4/letter/m/a6a055/32.png) [@Matt](https://forums.powershell.org/u/Matt)\
**Post date:** [November 10, 2022, 3:31am UTC](https://forums.powershell.org/t/stripping-sections-of-an-ou-out-to-populate-ad-fields/20879/6 "2022-11-10T03:31:15Z")

</div>

It might help to understand your goal a bit better.

The DN of just an OU looks like OU=AzureUsers,DC=contoso,DC=com  
The DN of a user looks like CN=Bugs Bunny,OU=AzureUsers,DC=constoso,DC=com

To confirm, based on your opening post, you are just concerned with DNs of users. Correct?

And it sounds like you want everything between CN=Bugs Bunny and DC=constoso,DC=com So just the OUs.

If that is the case, then you just need to strip off the first part CN=Bugs Bunny and the last part DC=constoso,DC=com. If this is your goal, then the fact that OU=something,OU=something etc may vary shouldn’t matter.

The next question would be, if the above is correct, then how did you want the data and where did you want to put it in AD?

example options  
option 1 = OU=something,OU=something,OU=something

options 2 = OU=something  
OU=something

---

<div class="post-metadata">

**Author:** ![SSHero](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/sshero/32/4058_2.png) [@SSHero](https://forums.powershell.org/u/SSHero)\
**Post date:** [November 10, 2022, 9:19am UTC](https://forums.powershell.org/t/stripping-sections-of-an-ou-out-to-populate-ad-fields/20879/7 "2022-11-10T09:19:42Z")

</div>

After looking at this with fresh eyes this morning I realized I could use @mawazo solution but use negative values inside the to work backwards in the OU’s which was the only issue with the inital response.

Thank you @mawazo & @Matt for your replies.

---

<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:16pm UTC](https://forums.powershell.org/t/stripping-sections-of-an-ou-out-to-populate-ad-fields/20879/8 "2024-05-16T20:16:24Z")

</div>

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


