Pure Powershell Post–Sample on How to use Custom PS Object to store information of different cmdlets and populate a collection to store into a CSV file


#STEPS FOR USING PS OBJECT TO STORE OBJECT PROPERTIES FROM DIFFERENT CMDLETS AND EXPORTING TO A CSV

# NOTE 1: This sample is an application of the PSObject method presented in this blog post :

# https://blogs.technet.microsoft.com/samdrey/2017/08/11/powershell-objects-useful-to-export-csv-information-coming-from-different-cmdlets/

#

# NOTE 2: You can copy paste that whole post to an ISE or a NOTEPAD and save it as a .ps1 script - the explanations are put in comments for that purpose 🙂

 

 

#STEP 1 - Get the list of items (mailboxes, recipients) either from a CSV file, or from an initial variable provisionning

#Below example I provision the object collection with "Get-Recipient" and I'll later call "Get-Mailbox" on each element of that collection to just get the e-mail addresses ...

$Recipients = Get-Recipient -RecipientType 'UserMailbox'

 

 

 

#STEP 2 - Initialize an empty array, to store the future collection of our custom PSObjects

$MyFutureCSV = @()

 

 

 

#STEP 3 - Define the structure of your PS Object in 2 sub-steps : define/initialize the properties of your custom PSObject and THEN create your cusatom PSObject using that defined/initialized properties

#See some details about PSObjects in my blog post :

#https://blogs.technet.microsoft.com/samdrey/2017/08/11/powershell-objects-useful-to-export-csv-information-coming-from-different-cmdlets/

 

#   STEP 3.1 - Define/Initialize the properties of your custom PSObject

$Props = @{

    "Name of Mailbox" = "";

    "Number of Items" = "";

    "Storage Limit" = "";

    "SMTP Adresses" = "";

}

 

#   STEP 3.2 - Create your custom PSObject and assigne the properties using the hash table created above

$MyCustomObject = New-Object -TypeName PSObject -Prop $props

 

 

 

#STEP 4 - using the collection created on #STEP 1 above, browse the collection item by item - Foreach ($Recipient in $Recipients) -

Foreach ($Recipient in $Recipients) {

    #  We get a first set of data - Get-MailboxStatistics - that we put in a variable to ease up the call of each property later

    $MailboxStats = Get-MailboxStatistics -Identity $Recipient.name | select DisplayName, itemcount,StorageLimitStatus

    #  We get a second set of data - Get-Mailbox - that we also put in a variable to ease up also the call of each property later

    $MAilboxAddresses = get-mailbox $Recipient.name | Select emailaddresses

 

 

 

#STEP 5 - Now the nice part : we assign each property we called from the 2 above cmdlets to our custom object, so we'll have everything we need/want in just one object, that combines info from 2 different cmdlets - Get-MAilboxStatistics and Get-Mailbox in our example

    #  NOTE: if the properties (that we defined/initialized on the hash table above) of your PSObject have spaces in it, you have to call them using double-quotes

    $MyCustomObject."Name of Mailbox" = $MailboxStats.DisplayName

    $MyCustomObject."Number of Items"= $MAilboxStats.ItemCount

    $MyCustomObject."Storage Limit" = $MailboxStats.StorageLimitSTatus

    #  NOTE: for multi-valued properties, just use the "-join <separator>" Powershell function like the below example

    $MyCustomObject."SMTP Adresses" = ($MailboxAddresses.emailaddresses -join ";")

 

 

 

#STEP 6 - append the custom object with the current properties in your collection variable ("+=" means "Object = Object + Stuff)

    $MyFutureCSV += $MyCustomObject

}

 

#To dump your object on screen :

$MyFutureCSV | ft -a

 

#To store your object in a CSV in the C:\temp directory:

$MyFutureCSV | Export-Csv -NoTypeInformation C:\temp\MyCSV.csv

 

#To store your object in a CSV in the current user's desktop - $Env:Userprofile\Desktop:

$MyFutureCSV | Export-Csv -NoTypeInformation $Env:Userprofile\Desktop\MyCSV.csv

 

 


Comments (0)

Skip to main content