Multiple cmdlets required for same report

Hi,

Unfortunately, my tests and research did not conclude in significant results…hence, I am here…posting.
I am trying to “combine” 2 cmdlets (because I do not have one which will give me what I want) and create a table.

One example I can use: in a .csv 2 columns are needed (PrimarySMTPAddress and ItemCount)
For PrimarySMTPAddress i need Get-mailbox
For ItemCount I need Get-mailboxstatistics

The script would generate a csv such as…

PrimarySMTPAddress; ItemCount
a@a.a;1
b@b.b;15
c@c.c;250

Sorry if it seems a noobish question…but I am a basic PS user.

BR
Adrian

Hi Adrian, Welcome to PowerShell.org!

This is a quite common question for beginners and the generic approach is the following:

  1. Import the CSV file
  2. For each entry imported perform this:
    2.1 Gather information from each source
    2.2 Create a data entry that combines information from multiple commands in (2.1)
    3.3 Return data entry
  3. Export data returned from (2) to CSV file

PowerShell is using objects of different types and there is a special type called PSCustomObject that can be used to create custom objects for storing data (called a data entry above).

An example of this could in your case be:

# Set paths:
$InputPath = 'C:\folder\importdata.csv'
$OutputPath = 'C:\folder\MyReport.csv'
# Import CSV:
$ImportedData = Import-Csv -Path $InputPath

# Perform something for each entry and store output in $Report
$Report = Foreach($Entry in $ImportedData)
{
    # Get first data:
    $Mailbox = Get-Mailbox -Identity $Entry.UserName 
    $PrimarySMTPAddress = $Mailbox.PrimarySMTPAddress.ToString()

    # Get second data:
    $Statistics = Get-MailboxStatistics -Identity $Mailbox
    $ItemCount = $Statistics.ItemCount

    # Combine data in a hashtable:
    $MyData = @{
        PrimarySMTPAddress = $PrimarySMTPAddress
        ItemCount          = $ItemCount
    }

    # Create new customobject based on $MyData and output that to pipeline:
    New-Object -TypeName PSCustomObject -Property $MyData
}

# Export data in $Report to file:
$Report | Export-Csv -Path $OutputPath -NoTypeInformation

The same thing could be performed in a number of different ways, I’ve tried to write an example that is easy to understand.

Thank you Simon.
This worked like a charm…think I understood