Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm trying to make use of the Common Parameter OutVariable in a simple one-liner PowerShell command, but it's not working the way I think it should (or really at all).

Get-Mailbox -OutVariable mBoxes | Get-MailboxStatistics | Select-Object DisplayName,TotalItemSize,@{name="ArchiveStatus";Expression={$mBoxes.ArchiveStatus}}

I simply want to return 3 columns, with the third column being a property of the results of the initial Get-Mailbox command. I tried to use OutVariable to store the results for use in my Select-Object command at the end, but that column is always empty when the results print.

Am I not understanding the purpose of OutVariable?

BTW, Get-Mailbox | Select ArchiveStatus works just fine.

jscott - I simplified the command to the following --

get-mailbox -OutVariable mBoxes | Select-Object @{name="Null?";Expression={$mBoxes -eq $null}}

I get True for every line that Get-Mailbox would return. It's clearly not storing anything in the $mBoxes variable.

share|improve this question
    
What does $mBoxes.getType() show? Is it ArrayList? If it is, you'll either need to index $mBoxes[0].ArchiveStatus or walk with foreach/ForEach-Object. If you have a hash, use .GetEnumerator. –  jscott May 23 '12 at 15:31
    
@uSlackr That's not how to use OutVariable. –  jscott May 23 '12 at 15:48
    
Oops, you are right. Deleted –  uSlackr May 23 '12 at 15:58

2 Answers 2

up vote 1 down vote accepted

Actually, I think it should work. You missing the fact, that -OutVariable is collection (and not simple array) and it's last element is usually what you are after.

Code that worked for me:

Get-Mailbox -OutVariable mBoxes | Get-MailboxStatistics | 
    select DisplayName, TotalItemSize, @{
         Name = 'ArchiveStatus'
         Expression = { $mBoxes.Item($mBoxes.Count - 1).ArchiveStatus
    }

That said - I recommend using slightly different technique with Add-Member for that. Can't test performance, but I guess it should be better than collecting whole output from Get-Mailbox...:

Get-Mailbox | Foreach-Object { Get-MailboxStatistics -Identity $_ |
    Add-Member -MemberType NoteProperty -Name ArchiveStatus -Value $_.ArchiveStatus -PassThru } |
    select DisplayName, TotalItemSize, ArchiveStatus
share|improve this answer
1  
To clarify -- the first example only works when running in a plain vanilla PowerShell window with the Exchange Snapin added. It will not work in Exchange Management Shell due to the fact that Get-Mailbox is a Function in EMS and not a Cmdlet as when you add the Snapin. –  pk. May 25 '12 at 13:27

Apparently I wasn't misunderstanding how OutVariable worked, the issue was due to the fact that I was using the Exchange Management Shell instead of PowerShell with a Snapin.

A somewhat analagous generic example of proper (albeit inane) usage is below --

do-something -OutVariable outVar | do-somethingelse | Select-Object Property1,Property2,@{name="CalculatedProperty";Expression={$outVar.Item($outVar.Count - 1).Property}}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.