tl;dr:
@($columns.psobject.properties).Count
What you're looking for is the count of an object's properties, which can be done via the .psobject.properties
collection, which is available on any object in PowerShell - and therefore also on the [pscustomobject]
instances returned by Import-Excel
- as a reflection feature that allows you to inspect a given object's properties.
Counting the properties is tricky, because the specific collection data type returned by .psobject.properties
- [System.Management.Automation.PSMemberInfoIntegratingCollection[System.Management.Automation.PSPropertyInfo]]
- unexpectedly implements neither a .Count
nor a .Length
property.
The workaround is to use @(...)
, the array subexpression operator, to collect the individual properties as a regular PowerShell array ([object[]]
), on which you can then call .Count
:
@($columns.psobject.properties).Count
See this GitHub issue, which asks for a .Count
property to be implemented on the collection returned by .psobject.properties
so as to render this workaround unnecessary.