Find out how to create clearly laid out CSV files with the help of PowerShell scripts: use PowerShell Custom Objects combined with Export-csv commandlet.
Index
Introducing PowerShell Custom Objects
Custom Objects are „generic“ objects which you can create yourself in PowerShell.
A custom object lets you define various properties which can be dynamically added to PowerShell scripts. A custom object can either have pre-defined properties like any other object or you can add them dynamically during runtime when needed.
There are many ways to use custom objects and turn them into helpful tools. You can either:
- maintain a „status variable“ in log files or
- transfer multiple messages simultaneously to a function without using too many variables.
Generating PowerShell Custom Objects
You create a PowerShell Custom Object with the commandlet “New-Object”.
1 |
$object = New-Object PSObject |
In this example, we created an object of the type „PSCustomObject“.
For now the initial object is empty and has no further features. You can add new features by using the commandlet “Add-Member”.
1 |
$object | Add-Member NoteProperty "Color" "Red" |
As a result, the object now has the feature „Color=red“. You can repeat this as many times as you like and give the object different features:
1 |
$object | Add-Member NoteProperty "Size" 1 |
Exporting Custom Objects as CSV
A major benefit is that you can transfer the entire object to other functions or commandlets:
1 |
$object Export-Csv -NoTypeInformation -Path .\log.csv -Delimiter ';' |
Or with a function:
1 2 3 4 5 |
function my-export { param($myInput) $myInput Export-Csv -NoTypeInformation -Path .\log.csv -Delimiter ';' } my-export $object |
Here we created a .csv file which looks like this:
Color | Size |
Red | 1 |
And accordingly, the text appears like this:
“Color”;”Size”
“Red”;”1″
You can also combine several objects into a list or an array to display them collectively:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$list = @() $object1 = New-Object PSObject $object1 | Add-Member NoteProperty "Name" "Alice" $object1 | Add-Member NoteProperty "Color" "Red" $object1 | Add-Member NoteProperty "Size" 1 $list += $object1 $object2 = New-Object PSObject $object2 | Add-Member NoteProperty "Name" "Bob" $object2 | Add-Member NoteProperty "Color" "Blue" $object2 | Add-Member NoteProperty "Size" 2 $list += $object2 $list | Export-Csv -NoTypeInformation -Path .\log.csv -Delimiter ';' |
As a result, your new .csv file automatically contains both objects:
Name | Color | Size |
Alice | Red | 1 |
Bob | Blue | 2 |
Accordingly, the text appears as:
“Name”;”Color”;”Size”
“Alice”;”Red”;”1″
“Bob”;”Blue”;”2″
In summary, PowerShell Custom Objects combined with Export-csv commandlet are an effective and useful tool to export sets of data to .csv files.
FirstAttribute AG – Microsoft Consulting Partner for
Migration and Active Directory
Leave a Reply
<p>Your email is safe with us.<br/>Information about our <a href="https://activedirectoryfaq.com/contact-us/">data protection policies</a></p>