What’s the Deal with PipelineVariable in PowerShell Commands?

0
1
Asked By TechieTurtle27 On

I'm having some trouble with the `PipelineVariable` feature in PowerShell, particularly with the `Get-Mailbox` and `Get-MailboxStatistics` commands. I'm trying to run the following command: `Get-Mailbox user -PipelineVariable mbx | Get-MailboxStatistics | Select TotalItemSize, TotalDeletedItemSize, @{N = 'ArchiveStatus'; E = {$mbx.ArchiveStatus}}`. From what I understand, this should allow me to get the `TotalItemSize`, `TotalDeletedItemSize`, and `ArchiveStatus`, but the `ArchiveStatus` is coming up blank. I thought the output from `Get-Mailbox` would be stored in `$mbx` and available further down the pipeline, but that doesn't seem to be working. Ideally, I want to pull stats from both commands, but only for specific mailbox sizes. I know I could work around this using a `ForEach` loop, but I thought this approach should work according to the documentation. Any insights?

4 Answers

Answered By HelpfulHarry99 On

It looks like you've stumbled upon a known issue with `PipelineVariable`. Some cmdlets, especially when using remote sessions, don’t populate them correctly. Have you tried this modified command? `Get-Mailbox user | Sort-Object -PipelineVariable mbx | Get-MailboxStatistics | Select TotalItemSize, TotalDeletedItemSize, @{N = 'ArchiveStatus'; E = { $mbx.ArchiveStatus } }`? I've had success with that approach!

CuriousCoder42 -

That actually does work for me too! Thanks for the tip!

Answered By ScriptSlinger88 On

You're right, and you've done your homework! Unfortunately, `PipelineVariable` has limitations with some cmdlets, especially those that rely on CDXML like `Get-Mailbox`. A more reliable way is to use a `ForEach` loop, creating a custom object for each mailbox instead. Here's a snippet on how to do it: `"user" | ForEach-Object { $mailbox = Get-Mailbox $_; $mailboxStats = $mailbox | Get-MailboxStatistics; [PSCustomObject]@{ TotalItemSize = $mailboxStats.TotalItemSize; TotalDeletedItemSize = $mailboxStats.TotalDeletedItemSize; ArchiveStatus = $mailbox.ArchiveStatus; } }`. It takes a bit more effort, but it gets the job done!

Answered By PowerfulPanda45 On

Just a heads-up: because Exchange uses an implicit session, it tends to run the common parameters in that remote session. This means that the `$mbx` variable is being set in a different context than what you're expecting. To fix it, you might want to try using a traditional loop or `ForEach-Object`. For instance: `Get-Mailbox | ForEach-Object { $mbx = $_; $_ } | ...` could be the key here.

Answered By ObjectOrientedOwl On

Building a custom object might be the way to go here. I recently shared a script on a related topic that could help: [Check it out here!](https://techbloggingfool.com/2025/06/30/powershell-report-to-avoid-hidden-exchange-mailbox-quota-violations/)

LoopLover21 -

Absolutely! Building an object instead of cramming everything into one command is definitely a better approach.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.