I have a script to list all AD OUs but for some reason the foreach is giving output of an integer of each OU
I run it and it outputs for example if there is 10 OUs
0
1
2
3
4
5
6
7
8
9
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") $objForm = New-Object System.Windows.Forms.Form $objForm.Text = "Select an OU" $objForm.Size = New-Object System.Drawing.Size(500,200) $objForm.StartPosition = "CenterScreen" $OKButton = New-Object System.Windows.Forms.Button $OKButton.AutoSize = $True $OKButton.Location = New-Object System.Drawing.Size(200,120) $OKButton.Text = "OK" $OKButton.Add_Click({$x=$objListBox.SelectedItem;$objForm.Close()}) $objForm.Controls.Add($OKButton) $objLabel = New-Object System.Windows.Forms.Label $objLabel.Location = New-Object System.Drawing.Size(10,20) $objLabel.Size = New-Object System.Drawing.Size(280,20) $objLabel.Text = "Please select an OU" $objForm.Controls.Add($objLabel) $objListBox = New-Object System.Windows.Forms.ListBox $objListBox.Location = New-Object System.Drawing.Size(10,40) $objListBox.Size = New-Object System.Drawing.Size(460,20) $objListBox.Height = 80 $OUlist = (Get-ADOrganizationalUnit -Filter *) ForEach ($OU in $OUlist) {$objListBox.Items.Add($OU.DistinguishedName)} $objForm.Controls.Add($objListBox) $objForm.Topmost = $True $objForm.Add_Shown({$objForm.Activate()}) [void] $objForm.ShowDialog() $NewOU = $objListBox.SelectedItem $NewOU
Thanks
Daniel