Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get powershell object properties in the same order that format-list does?

Tags:

powershell

I'm writing some reporting scripts in Powershell and collecting up a summary table of items as a blank object with additional properties added one by one:

$cmClusters = @()

foreach ($Cluster in Clusters) {

    $cmCluster = New-Object System.Object
    $cmCluster | Add-Member -type NoteProperty -Name VC -Value $strVC       
    $cmCluster | Add-Member -type NoteProperty -Name Name -Value $Cluster.name
    # etc...

    $cmClusters += $cmCluster;

}

If I just dump $cmClusters at the end of this, I get a format-list output with the properties in the order that I added them.

However, I was hoping to write a generic "dump this collection of objects to an excel tab" function to produce my report, which will be several similar worksheet tabs from different lists of objects.

That looks like this:

function DumpToExcel($workbook, $tabTitle, $list)
{   
    $sheet = $workbook.worksheets.add()     
    $sheet.Name = $tabTitle

    $col = 1
    $row = 1
    $fields = $list[0] | Get-Member -MemberType NoteProperty | Select-Object *

    Foreach ($field in $fields) {
        $sheet.cells.item($row,$col++) = $field.Name        
    }   

    $heading = $sheet.UsedRange
    $heading.Font.Bold = $True

    $row++
    Foreach ($cmCluster in $list) {
        $col=1
        Foreach ($field in $fields) {
            $sheet.cells.item($row,$col++) = $cmCluster.($field.Name)
        }
        $row++
    }

    $sheet.UsedRange.EntireColumn.AutoFit() | Out-Null
}

which works, but the property names are now in alphabetical order.

What can I use to get my list of properties in the same order that Format-List does?

like image 621
AnotherHowie Avatar asked Dec 08 '14 15:12

AnotherHowie


People also ask

How do I get the properties of a PowerShell object?

To get the properties of an object, use the Get-Member cmdlet. For example, to get the properties of a FileInfo object, use the Get-ChildItem cmdlet to get the FileInfo object that represents a file. Then, use a pipeline operator ( | ) to send the FileInfo object to Get-Member .

How do I sort in ascending order in PowerShell?

Sort-Object sorts the list in the default order, ascending. The Get-Content cmdlet uses the Path parameter to specify the directory and filename.

What PowerShell command selects specific properties from an object?

The Select-Object cmdlet selects specified properties of an object or set of objects. It can also select unique objects, a specified number of objects, or objects in a specified position in an array. To select objects from a collection, use the First, Last, Unique, Skip, and Index parameters.

How do I use AutoSize in PowerShell?

If you specify the AutoSize parameter when you run the Format-Table command, PowerShell calculates column widths based on the actual data displayed. This makes the columns readable. The Format-Table cmdlet might still truncate data, but it only truncates at the end of the screen.


1 Answers

Try this:

$fields = $list[0].psobject.properties | select name
like image 170
mjolinor Avatar answered Oct 17 '22 19:10

mjolinor