I'm trying to print out only the property names of a Powershell object.
In a script I do an Invoke-RestMethod
and Write-Host ($response.result | Format-List | Out-String)
gives me a nice list of the $response.result
object.Get-Member -InputObject $response.result
also does not display what I want.$response.result
looks something like this: @{id=1; skip=true}
.
How do I just get a list/table thats shows id, skip
etc.
Many thanks!
Ideally your script would create your objects ( $obj = New-Object -TypeName psobject -Property @{'SomeProperty'='Test'} ) then just do a Write-Output $objects . You would pipe the output to Format-Table .
To get the object properties, the Get-member cmdlet is used in PowerShell. Specify a cmdlet, use the pipeline operator, and then type the Get-Member cmdlet to see all of the properties available from the specified command.
To select objects from a collection, use the First, Last, Unique, Skip, and Index parameters. To select object properties, use the Property parameter.
If the result is just a simple 1-level hashtable, you could do something like:
(@{id=1; skip=$true}).GetEnumerator() | %{ $_.Key }
id
skip
All PowerShell objects have a hidden property PSObject
that allows accessing information about the object, e.g. its properties:
$response.result.PSObject.Properties | Select-Object -Expand Name
If it's not a hashtable, you can use Get-Member to find the properties like this:
$response.result | Get-Member -MemberType Properties | Select-Object Name
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With