Numbered output

This topic contains 7 replies, has 4 voices, and was last updated by  Olaf Soyk 1 week, 6 days ago.

  • Author
    Posts
  • #75967

    Craig
    Participant

    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

  • #75973

    Olaf Soyk
    Participant

    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/

  • #75992

    Richard Siddaway
    Moderator

    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

    • #76042

      Kevyn
      Participant

      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.

  • #76039

    Kevyn
    Participant

    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
    
    • #76045

      Olaf Soyk
      Participant

      Why make it more complex than necessary?

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

    Kevyn
    Participant

    That's definitely shorter. 🙂

    • #76707

      Olaf Soyk
      Participant

      I just found another one ...

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

You must be logged in to reply to this topic.