1

newbie PS user here.

I'm using Get-ADOrganizationalUnit to return child OU's that I want as a string array. IE "ou1","ou2","ou3", etc. The example I found returns the data in a table form. I have tried using export-csv, but that did not work well at all! Here is what I am using to get the data:

$Items = Get-ADOrganizationalUnit -LDAPFilter '(name=*)' -SearchBase 'OU=aa,OU=bb,OU=cc,OU=dd,DC=AD,DC=contoso,DC=CA' -SearchScope OneLevel | Format-Table Name -HideTableHeaders

this returns:

ThisOU

ThatOU

AnotherOU

etc.

But I need it structured as "ThisOU","ThatOU","AnotherOU",Etc The string array will be used in another script to create a drop down list of OUs. Any assistance is GREATLY appreciated!

10
  • 1
    Don't Format-Table (or any other Format-*) unless you absolutely have to. Commented Mar 23, 2018 at 18:35
  • Why are you trying to format the output? Get-ADOrganizationalUnit returns objects, not text. Commented Mar 23, 2018 at 18:35
  • I was using the example from scconfigmgr.com, hoping to change the output to a string array. I also tried using select Name, instead Format-Table Name Commented Mar 23, 2018 at 18:36
  • What kind of output do you want? A list of just the OU names? Commented Mar 23, 2018 at 18:50
  • I'd like a string array. I have a script to run during SCCM task sequence where the front line tech would be presented with a drop down list of the available OU's for the newly imaged system to be placed in. That script requires 4 parameters, one of which is a string array of OUs. Commented Mar 23, 2018 at 18:53

1 Answer 1

1

Instead of piping through Format-Table (or Select-Object), use the Foreach-Object (foreach) cmdlet to extract the name property into a string array.

[string[]] $items = Get-ADOrganizationalUnit -LDAPFilter '(name=*)' -SearchBase 'OU=aa,OU=bb,OU=cc,OU=dd,DC=AD,DC=contoso,DC=CA' | foreach Name

If you then want to turn this array of strings into a single string along the lines of "name1","name2","name3", you can do:

$formattedNames = "`"$($items -join '","')`""
Sign up to request clarification or add additional context in comments.

1 Comment

The second part is definitely more elegant than my foreach line. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.