Create a Custom PowerShell Object to Simplify Appending CSV files

Doctor Scripto

Summary: Microsoft Scripting Guy, Ed Wilson, shows how to create a custom Windows PowerShell object to simplify appending to CSV files.  Hey, Scripting Guy! Question Hey, Scripting Guy!  Yesterday, you talked about creating two objects, exporting them to CSV files, and then manipulating the strings to combine the CSV files. Is there a cleaner way of doing this? —CS Hey, Scripting Guy! Answer Hello CS, Microsoft Scripting Guy, Ed Wilson, is here. This morning I am sitting on the lanai and sipping a cup of Assam black tea. I brought the leaves back from Munich, Germany (by the way, if you bring back several bags of tea leaves, be prepared for additional customs screening when you return to your home country—at least that is what happened to me). The tea is a robustly flavored black tea. I added a few organic rose petals to the pot to give it a nice, mellow flavor. CS, of course, there are other ways of combining WMI objects and creating CSV formatted output. In Windows PowerShell, there are always multiple ways of accomplishing the same task.

First create a custom object

Note   Today’s post is basically a continuation of yesterday’s blog post, Use PowerShell to Add Two Pieces of CSV Data Together. It might be a good idea to read that article first. The first step is to create a couple of custom objects. The easiest way to create a custom object is to use the Select-Object cmdlet. This cmdlet permits easy conversion from one type of object to a custom object. In the custom object, the properties and values appear as noteproperty data types, as shown here.

PS C:> $osInfo = gwmi win32_operatingsystem |

    select version, caption, serialnumber, osarchitecture

PS C:> $osInfo | gm -MemberType NoteProperty

   TypeName: Selected.System.Management.ManagementObject

 

Name           MemberType   Definition                                       

—-           ———-   ———-                                       

caption        NoteProperty System.String caption=Microsoft Windows 8 Pro    

osarchitecture NoteProperty System.String osarchitecture=64-bit              

serialnumber   NoteProperty System.String serialnumber=00178-10777-21922-AA381

version        NoteProperty System.String version=6.2.9200         Because I am already dealing with an object, I can easily extend that object by using the Add-Member cmdlet. The Add-Member cmdlet adds additional members to an existing object thereby permitting the extending of existing objects. I have my computer information stored in a custom object in a variable—just as I also have the operating system information stored in a custom object. The Windows PowerShell code to create the two objects is shown here.

$computer = gwmi win32_computersystem |

    select numberOfProcessors, NumberOfLogicalProcessors, HypervisorPresent

$osInfo = gwmi win32_operatingsystem |

    select version, caption, serialnumber, osarchitecture 

Extending an existing object

If I wanted to add only one or two properties to an existing object, I might do it manually. But because I already have an object—with an undetermined number of properties—I want an automatic way to add the members to the object. To do this, I use the Get-Member cmdlet to find the members of the object I wish to locate and to use. As illustrated earlier, the properties I am interested in obtaining are NoteProperty member types. Therefore, I use the Foreach statement to walk through the members of the custom object stored in the $osInfo variable—but only if -MemberType is a NoteProperty data type. This line of code is shown here.

Foreach($p in Get-Member -InputObject $osInfo -MemberType NoteProperty) Now, I need to use the Add-Member cmdlet to add the NoteProperty member types to the custom object stored in the $computer variable. I use the object stored in the $computer variable for the InputObject, and I add the new members to the object. This line of code is shown here (the back tick is line continuation—I broke the line for readability). The subexpression is required to obtain the value stored in each property.

Add-Member -InputObject $computer -MemberType NoteProperty `

  -Name $p.Name -Value $osInfo.$($p.Name) -Force Once modified, the new object easily creates CSV output when piped to the ConvertTo-CSV cmdlet (or to the Export-CSV cmdlet). The complete Get-OsAndComputerInfoAddMember.ps1 script is shown here.

Get-OsAndComputerInfoAddMember.ps1

$computer = $osInfo = $null

$computer = gwmi win32_computersystem |

    select numberOfProcessors, NumberOfLogicalProcessors, HypervisorPresent

$osInfo = gwmi win32_operatingsystem |

    select version, caption, serialnumber, osarchitecture

Foreach($p in Get-Member -InputObject $osInfo -MemberType NoteProperty)

{

 Add-Member -InputObject $computer -MemberType NoteProperty `

  -Name $p.Name -Value $osInfo.$($p.Name) -Force

}

$computer | ConvertTo-Csv -NoTypeInformation

  CS, that is all there is to using Windows PowerShell to create a custom object and append to CSV data. Join me tomorrow when I will talk about cool Windows PowerShell stuff. I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace. Ed Wilson, Microsoft Scripting Guy

0 comments

Discussion is closed.

Feedback usabilla icon