Home > Back-end >  Convert Output or Object in Array to Table Powershell
Convert Output or Object in Array to Table Powershell

Time:07-30

I created a custom object

$customsa = New-Object -TypeName psobject
$customsa | Add-Member -MemberType NoteProperty -Name Name -Value (Get-AzStorageAccount).StorageAccountName 
$customsa | Add-Member -MemberType NoteProperty -Name Tier -Value (Get-AzStorageAccount).AccessTier
$customsa | Add-Member -MemberType NoteProperty -Name Replication -Value (Get-AzStorageAccount).sku.Name
$customsa | Add-Member -MemberType NoteProperty -Name AccountKind -Value (Get-AzStorageAccount).kind
$customsa | Add-Member -MemberType NoteProperty -Name ResourceGroupName -Value (Get-AzStorageAccount).ResourceGroupName

The output is an array format

Output

However I want the output to show in a table format like this

Desired Table format

Thanks

CodePudding user response:

Have you tried?:

$customsa | Format-Table
# or
$customsa | Out-GridView

From your example:

$customsa = New-Object -TypeName psobject
$customsa | Add-Member -MemberType NoteProperty -Name Name -Value (Get-AzStorageAccount).StorageAccountName 
$customsa | Add-Member -MemberType NoteProperty -Name Tier -Value (Get-AzStorageAccount).AccessTier
$customsa | Add-Member -MemberType NoteProperty -Name Replication -Value (Get-AzStorageAccount).sku.Name
$customsa | Add-Member -MemberType NoteProperty -Name AccountKind -Value (Get-AzStorageAccount).kind
$customsa | Add-Member -MemberType NoteProperty -Name ResourceGroupName -Value (Get-AzStorageAccount).ResourceGroupName

$customsa | Format-Table

CodePudding user response:

Get-AzStorageAccount is returning all of your storage accounts each time you run it so you are assigning the value of all storage accounts to each property when you're using Add-Member.

You should be able to get the same result using Select-Object which will create your psobject for you

Get-AzStorageAccount |
    Select-Object StorageAccountName, AccessTier, Kind, ResourceGroupName, @{name = 'SKU'; expression = { $_.sku.name }} |
        Format-Table
  • Related