Struggling with PipelineVariable in PowerShell: What’s the Deal?

0
31
Asked By CuriousCoder42 On

I'm trying to figure out how to use PipelineVariable in PowerShell when working with Get-Mailbox and Get-MailboxStatistics. My command looks like this: `Get-Mailbox user -PipelineVariable mbx | Get-MailboxStatistics | Select TotalItemSize,TotalDeletedItemSize,@{N = 'ArchiveStatus'; E = {$mbx.ArchiveStatus}}`. From what I've read, this should allow me to pull the ArchiveStatus from the Get-Mailbox output, but it's coming up blank. I thought that the PipelineVariable would let me reference $mbx throughout the pipeline. My goal is to gather data from both commands for mailboxes of a specific size, but I really want to make the PipelineVariable approach work if possible. Any insights?

3 Answers

Answered By ScriptingSensei On

You're on the right track, but there are some limitations with PipelineVariables, especially when using CDXML cmdlets like Get-Mailbox. I've faced similar issues, so I usually revert to a more manual approach. Using a ForEach loop might be a better option: `"user" | ForEach-Object { $mailbox = Get-Mailbox $_; $mailboxStats = $mailbox | Get-MailboxStatistics; [PSCustomObject]@{ TotalItemSize = $mailboxStats.TotalItemSize; TotalDeletedItemSize = $mailboxStats.TotalDeletedItemSize; ArchiveStatus = $mailbox.ArchiveStatus; } }` It’s a bit more work, but it gets the job done!

Answered By CodeCraftsman On

Building an object instead of trying to get everything in one line is definitely the way to go. Check out this recent script I put together that demonstrates how to do it effectively. Here’s the link: https://techbloggingfool.com/2025/06/30/powershell-report-to-avoid-hidden-exchange-mailbox-quota-violations/

DevDynamo -

I agree! It’s better to break it down instead of cramming everything into a one-liner. Just remember not to use `$Report += $MailboxTotalSize`, as it's not the most efficient way to handle arrays!

Answered By TechGuru888 On

There seems to be a known bug with PipelineVariable where certain cmdlets, like Get-Mailbox, don't play nicely with it. You might want to try adjusting your command to see if it works better like this: `Get-Mailbox user | Sort-Object -PipelineVariable mbx | Get-MailboxStatistics | Select TotalItemSize, TotalDeletedItemSize, @{N = 'ArchiveStatus'; E = { $mbx.ArchiveStatus } }` I've tested it, and it returns the expected results.

CuriousCoder42 -

Thanks for the tip! I tried your method, and it worked perfectly!

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.