Author Posts

July 26, 2017 at 9:35 pm

Hello,

This is the output I'm trying to accomplish (treat the periods as white-space please):
Number.......Name
————-.......———
1....................OU1
2....................OU2
3....................OU3

I feel like using Get-ADOrganizationalUnit (with appropriate -Filter, -SearchBase, and -SearchScope) and piping to Format-Table with a hash table would be the route to go, but I am a rookie with hash tables. Any tips?

Thanks,
Craig

July 26, 2017 at 10:25 pm

but I am a rookie with hash tables. Any tips?

The simplest solution for you coud be to search for an example without index and add it then .... 😉 My recommendation would be to use a custom object rahter than a hash table.

https://blogs.technet.microsoft.com/heyscriptingguy/2013/11/07/a-powershell-object-lesson-part-3/

July 27, 2017 at 10:07 am

Hash tables store their data in a random order so they won't work in the way you want. Creating a custom object is probably your best bet

July 28, 2017 at 8:17 am

Try the following:

$OUs = Get-ADOrganizationalUnit -Filter *
$Table = @()
For($i = 1;$i -le $OUs.Count;$i++)
{
  $TableObject = New-Object -TypeName PSObject
  $TableObject | Add-Member -MemberType NoteProperty -Name Selection -Value ($i -as [string])
  $TableObject | Add-Member -MemberType NoteProperty -Name OU -Value $OUs[$i - 1].DistinguishedName
  $Table += $TableObject
}
Write-Output $Table

July 28, 2017 at 8:24 am

Richard: When I was looking into this issue, I cam across an article that Don Jones wrote back in 2012 (https://technet.microsoft.com/en-us/library/hh750381.aspx). I found two things I thought I should mention that maybe you can relay to him.

1. In all of his Add-Member command, there is no space between the -MemberType parameter and the NoteProperty value that follows it (See example line below). Don't know if he can correct those entries

$object | Add-Member –MemberTypeNoteProperty –Name OSBuild –Value $os.BuildNumber

2. The article is about powershell, and all of the code is powershell code, but the code, for some reason, shows up under boxes labeled "VB" (Visual Basic, of course). Not sure if that's intentional (i.e. he had no choice) or if it's correctable, so that any possible confusion for readers could be avoided.

July 28, 2017 at 8:28 am

Why make it more complex than necessary?

Get-ADOrganizationalUnit -Filter * | 
    ForEach-Object {
        [PSCustomObject]@{
            Number = $Counter++
            Name = $_.Name
        }
    }

July 28, 2017 at 9:04 am

That's definitely shorter. 🙂

August 4, 2017 at 3:05 pm

I just found another one ...

Get-ADOrganizationalUnit -Filter * -OutVariable List | 
    Select-Object -Property Name,@{Name='Index';Expression={[ARRAY]::IndexOf($List,$_)}}