Multiple cmdlets required for same report

Welcome Forums General PowerShell Q&A Multiple cmdlets required for same report

This topic contains 2 replies, has 2 voices, and was last updated by

3 years, 11 months ago.

  • Author
  • #20918

    Points: 0
    Rank: Member


    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

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


  • #20919

    Points: 0
    Rank: Member

    Hi Adrian, Welcome to!

    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.

  • #20924

    Points: 0
    Rank: Member

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

The topic ‘Multiple cmdlets required for same report’ is closed to new replies.