Adding sequence number column to a result

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?

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

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

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

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.

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 :slight_smile: I was wondering what people are using to solve these kind of problems, what approach they find easy to understand and relate to.

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. :slight_smile:

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

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.

Thanks once again!

You’re the best =]