Solved

Powershell Get-ADOrganizationalUnit command not displaying results properly.

Posted on 2014-12-05
4
781 Views
Last Modified: 2014-12-05
I'm trying to run a simple Powershell script that gets the Active Directory OUs. The script is getting the results I want but it's not displaying them properly.  It's displaying  as "Object[ ] Array" Instead of the OU name (Also see screenshot).

Now if I output the results to a text file.. everything displays properly in the text file. Is there something I'm doing wrong here?

My code is below.
	$ADResults = Get-ADOrganizationalUnit -LDAPFilter '(name=*)' -SearchBase 'OU=Computers,OU=Main,DC=ad,DC=test,DC=net' -SearchScope OneLevel
	foreach ($items in $ADResults)
	{ $ADStartOU.Items.Add($ADResults) }

Open in new window

powershellresults.png
0
Comment
Question by:AnthonySmithMCP
  • 2
4 Comments
 
LVL 16

Expert Comment

by:Rajitha Chimmani
ID: 40482997
Guess you are using powershell forms and you have not provided the entire code how you are feeding the OUs to the drop down box. Please post the entire code.
0
 
LVL 84

Accepted Solution

by:
oBdA earned 500 total points
ID: 40483034
Main error: in the ForEach loop, you're adding the complete $ADResults array to $ADStartOU, instead of the loop item.
Then it's not clear where and how "$ADStartOU" is defined.
Finally, which OU property you actually want in the output; I'm guessing the DN?
Try
$ADResults = Get-ADOrganizationalUnit -LDAPFilter '(name=*)' -SearchBase 'OU=Computers,OU=Main,DC=ad,DC=test,DC=net' -SearchScope OneLevel
ForEach ($OU In $ADResults) {
	"Found OU: '$($OU.DistinguishedName)'" | Write-Host
	$ADStartOU.Items.Add($OU.DistinguishedName)
}

Open in new window

0
 
LVL 1

Author Comment

by:AnthonySmithMCP
ID: 40483095
Thank you oBdA,

I changed it to the code below. However the results is  giving the full distinguished name.. All i want is the name of the OU. is there anyway to filter it?

	$ADResults = Get-ADOrganizationalUnit -LDAPFilter '(Name=*)' -SearchBase 'OU=Computers,OU=Main,DC=ad,DC=test,DC=net' -SearchScope OneLevel
	foreach ($items in $ADResults)
	{ $ADStartOU.Items.Add($items) }

Open in new window

0
 
LVL 1

Author Comment

by:AnthonySmithMCP
ID: 40483110
Got it working with the code below. Thank you soooo much
	$ADResults = Get-ADOrganizationalUnit -LDAPFilter '(name=*)' -SearchBase 'OU=Computers,OU=Main,DC=ad,DC=test,DC=net' -SearchScope OneLevel
	ForEach ($Items In $ADResults)
	{
		$ADStartOU.Items.Add($items.Name)
	}

Open in new window

0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Last week, our Skyport webinar on “How to secure your Active Directory” (https://www.experts-exchange.com/videos/5810/Webinar-Is-Your-Active-Directory-as-Secure-as-You-Think.html?cid=Gene_Skyport) provided 218 attendees with a step-by-step guide for…
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question