Adding sequence number column to a result

Tagged: 

This topic contains 9 replies, has 4 voices, and was last updated by  Vandrey Trindade 3 years, 6 months ago.

  • Author
    Posts
  • #13953

    Vandrey Trindade
    Participant

    Hi,

    I'm using the command below to get a list of some accounts on our Exchange Online:

    get-mailbox | ?{$_.ArchiveQuota -like "25*"}

    The result is:

    Name Alias ServerName ProhibitSendQuota
    —- —– ———- —————
    user.martins user.martins b22prd822222 24.75 GB (26...
    user.sousa user.sousa b22prd822222 24.75 GB (26...
    user.ferreira user.ferreira b22prd822222 24.75 GB (26...
    user.pereira user.pereira b22prd822222 24.75 GB (26...
    user user b22prd822222 24.75 GB (26...

    I want to organize that result adding a sequence number, looking like this:

    Number Name Alias ServerName ProhibitSendQuota
    —— —- —– ———- —————
    1 user.martins user.martins b22prd822222 24.75 GB (26...
    2 user.sousa user.sousa b22prd822222 24.75 GB (26...
    3 user.ferreira user.ferreira b22prd822222 24.75 GB (26...
    4 user.pereira user.pereira b22prd822222 24.75 GB (26...
    5 user user b22prd822222 24.75 GB (26...

    Is it possible? Even using "Sort" for Name? No matter how many lines the result could have?

  • #13955

    Dave Wyatt
    Moderator

    Sure, that can be done. If you want to sort the output, though, you'll wind up holding the entire result set in memory temporarily, which may cause an OutOfMemory error.

    Here's a basic example of adding a "Number" property to objects in the pipeline:

    Get-ChildItem -File |
    ForEach-Object -Begin { $index = 0 } -Process {
        Add-Member -InputObject $_ -MemberType NoteProperty -Name Number -Value (++$index) -PassThru
    } |
    Format-Table -Property Number,Mode,LastWriteTime,Length,Name
    
  • #13961

    Stephen Owen
    Participant

    I love this example Dave. Very very simple and much better than my old method of doing it.

  • #13962

    Tore Groneng
    Participant

    hi,

    I try to spread love and understanding about powershell anywhere and any time. Every time I start using Add-Member, people either love it(about 10%) or ask me if there is another way. In my bag of tricks I usually pull something like this out:


    $files = Get-ChildItem -file
    $index = 0
    foreach ($file in $files)
    {
    $FileItem = $file | select Number,*
    $fileitem.Number = ++$index
    $fileitem
    }

    What do you guys think?

    Cheers

    Tore

  • #13963

    Dave Wyatt
    Moderator

    The only reason I would choose that approach (several extra lines of code, not quite as easily read) is if it performed significantly better, and I wanted to optimize the code to perform well over a large data set. Based on my tests just now, though, it actualy took about 3 times as long to use the Select-Object approach.

  • #13966

    Tore Groneng
    Participant

    yes, I know there is a performance hit. That was not the point. Sure, I have "several" extra lines of code, however my code example with the same select you have, is actually 36 characters less to type 🙂 I was wondering what people are using to solve these kind of problems, what approach they find easy to understand and relate to.

  • #13967

    Dave Wyatt
    Moderator

    I didn't use aliases or positional parameters in my example the way you did with Select-Object, and I also used PowerShell 2.0-compatible syntax. If character count matters, that line could be written like this in version 3.0 or later:

        $_ | Add-Member Number (++$index) -pa
    

    Personally, I prefer to use the full syntax, for code clarity and all. 🙂

  • #13975

    Vandrey Trindade
    Participant

    Hi Dave,

    Thanks a lot for the fast reply. Worked like a charm!
    But there's a big space on front of the columns, do you know why?

    PS C:\Office 365> get-mailbox | ?{$_.ArchiveQuota -like "50*"} | sort name |
    >> ForEach-Object -Begin { $index = 0 } -Process {
    >> Add-Member -InputObject $_ -MemberType NoteProperty -Name Number -Value (
    ++$index) -PassThru
    >> } |
    >> Format-Table -Property Number,Name | more
    >>

    Number Name
    ------ ----
    1 user
    2 user.chagas
    3 user.pereira
    4 user.coelho
    5 user.oliveira
    6 user.scato
    7 user.souza
    8 user.falet
    9 user.queda
    10 user.silva
    11 user.aman

  • #13976

    Dave Wyatt
    Moderator

    You can add the -AutoSize switch when calling Format-Table to address that. Like sorting the list, this requires that the entire result set be temporarily held in memory, but since you're already doing that, that's not a big deal.

  • #13977

    Vandrey Trindade
    Participant

    Thanks once again!

    You're the best =]

You must be logged in to reply to this topic.