Share via


How to use New-Object PSObject

Question

Wednesday, February 4, 2015 1:14 AM

$(Foreach ($mailbox in Get-Recipient -ResultSize unlimited -RecipientType UserMailbox) {
    $stat = $mailbox | Get-MailboxStatistics | Select TotalItemSize
    New-Object PSObject @{
        FirstName = $mailbox.FirstName
        LastName = $mailbox.LastName
        DisplayName = $mailbox.DisplayName
        TotalItemSize = ($stat.TotalItemSize/1.0GB)
        PrimarySmtpAddress = $mailbox.PrimarySmtpAddress
        Alias = $mailbox.Alias
        }
}) | Select FirstName,LastName,DisplayName,TotalItemSize,PrimarySmtpAddress,Alias |
Export-CSV C:\MailboxReport.csv -NoTypeInformation

The above code is returning:

FirstName,LastName,DisplayName,TotalItemSize,PrimarySmtpAddress,Alias
,,,,,
,,,,,
,,,,,
,,,,,
,,,,,
,,,,,
,,,,,
,,,,,
,,,,,

Do I need something like this:

 $object = New-Object PSObject
      Add-Member -InputObject $object ...

I'm trying to compile a report with data gathered from Exchange 2007 on Server 2003 x64 using PowerShell 1.0.

All replies (2)

Wednesday, February 4, 2015 1:54 AM ✅Answered

Get-Mailbox |
    ForEach-Object{
        $mailbox=$_
        $TotalItemSize=$mailbox|Get-MailboxStatistics | Select -ExpandProperty TotalItemSize
        New-Object PSObject -Property @{
            FirstName = $mailbox.FirstName
            LastName = $mailbox.LastName
            DisplayName = $mailbox.DisplayName
            TotalItemSize=$mailbox.TotalItemSize.ToGb()
            PrimarySmtpAddress=$mailbox.PrimarySmtpAddress
            Alias = $mailbox.Alias
        }
    }

¯\(ツ)_/¯


Thursday, February 5, 2015 10:24 AM ✅Answered

Hi Jott,

Please add the parameter "-property" in the cmdlet "New-Object" like "New-Object PSObject -property @{", because the property header is name and value, so you will get null when you filter the properies with "select".

You can test the result with the script below:

Foreach ($mailbox in Get-Recipient -ResultSize unlimited -RecipientType UserMailbox) {
    $stat = $mailbox | Get-MailboxStatistics | Select TotalItemSize
    New-Object PSObject @{
        FirstName = $mailbox.FirstName
        LastName = $mailbox.LastName
        DisplayName = $mailbox.DisplayName
        TotalItemSize = ($stat.TotalItemSize/1.0GB)
        PrimarySmtpAddress = $mailbox.PrimarySmtpAddress
        Alias = $mailbox.Alias
        }
}

If there is anything else regarding this issue, please feel free to post back.

Best Regards,

Anna Wang

Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Support, contact [email protected]