Retrieve a shortened output from a field in PowerShell

I use a powershell command to retrieve information for all computers in my domain.
One of the fields is Parent container, that I labeled as location.


It returns something like


Is it possible to have it replace "Domain/OU/OU/Computers" with "Computers"?
Who is Participating?
QlemoConnect With a Mentor Batchelor and DeveloperCommented:
Of course. One way is to split at "/", and only take the last part:

Open in new window

Another option you could use get-qadobject and get the name of the OU

@{label=”Location”;expression={ (get-qadobject $($_.ParentContainerDN)).name}}
LindySAuthor Commented:
That almost works. The problem is that some of the computer OUs have sub OUs.
Is there a way to remove everything before the name of one of the parent OUs?

Like this example
Domain/OU/OU/Computers. Would it be possible to just strip the portion containing Domain?

I played with a few variations of what you gave me but didn't have any luck.
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

In that senerio I think what Qlemo posted would be best. Dependning on how your OU strucrture is you may need a few if statements to make sure you are getting the correct name and just filter out.
QlemoBatchelor and DeveloperCommented:
Do you want to get  OU/OU/Computers  for the above example?
LindySAuthor Commented:
This is the best solution.
I had to change it to "@{label=”Location”;expression={$_.ParentContainer.Split("/")[-2]}} ", but for the most part it gets me what I need.

There are only a hand full of computers that are outside of the normal structure.

Thanks for the help!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.