# Create custom table for showing results

**URL:** <https://forums.powershell.org/t/create-custom-table-for-showing-results/1028>\
**Category:** PowerShell Help\
**Created:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/create-custom-table-for-showing-results/1028 "2011-12-31T18:30:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![archives](https://avatars.discourse-cdn.com/v4/letter/a/bb73d2/32.png) [@archives](https://forums.powershell.org/u/archives)\
**Post date:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/create-custom-table-for-showing-results/1028/1 "2011-12-31T18:30:00Z")

</div>

by andrewv88 at 2012-12-04 22:59:41

> hello,   
> i tried to get my output in table format wihtout any success,  
>   
> [code2=powershell]$vms = "testvm", "testvm1"  
>   
> foreach ($vm in $vms){  
>   
> $dbname = (get-datastore -vm $vm).name  
> $dbsize = (get-datastore -vm $vm).CapacityGB  
> $vmos = (get-vmguest -vm $vm).OSFullName  
>   
> }[/code2]  
>   
>   
> i want to get my output in this format   
> [code2=powershell]name dbname dbsize OS  
> -------- ------------- -------------- --------  
> testvm nfs\_w2k8 1.2Tb Windows 2008R2  
> testvm1 nfs\_w2k8 1.2Tb Windows 2008R2[/code2]  
>   
> thanks

by nohandle at 2012-12-05 00:58:03

> You have to create a custom object. If you are on powershell v2 I’l recommend the Select-object way beause it sorts properties unlike the new-object using hashtable as property.   
> Excellent article by Don to learn how to do it: [http://technet.microsoft.com/en-us/maga … 50381.aspx](http://technet.microsoft.com/en-us/magazine/hh750381.aspx)

by nohandle at 2012-12-05 01:03:40

> Here is the fish. 🙂   
> `"" | Select-Object @{n="name";e={$vm}},@{n="dbName";e={$dbname}},@{n="dbSize";e={$dbsize}},@{n="OS";e={$vmos}}`

by andrewv88 at 2012-12-05 01:49:52

> i tried to do new object   
>   
> [code2=powershell]$vms = "testvm", "testvm1"  
>   
> $object = new-object Object  
>   
> foreach ($vm in $vms){  
>   
> $object | add-member NoteProperty "DB Name" $vm   
> $object | add-member NoteProperty "DB Name" (get-datastore -vm $vm).name  
> $object | add-member NoteProperty "DB Size" (get-datastore -vm $vm).CapacityGB  
> $object | add-member NoteProperty "OS Name" (get-vmguest -vm $vm).OSFullName  
>    
> }[/code2]  
>   
> but input has only the property of last vm

by RichardSiddaway at 2012-12-05 04:20:24

> Its because you aren’t outputting the object you create. You overwrite on each loop through foreach

by andrewv88 at 2012-12-05 04:24:48

> i tried to use array for saving $obj for each loop, outside the foreach loop  
>   
> $myArray += $obj  
>   
> it wasn’t work

by nohandle at 2012-12-05 04:41:28

> I think the solution shown by me should work when placed inside the foreach loop.

by andrewv88 at 2012-12-05 04:45:13

> **i solved it** , but the way is not good enough.  
>   
>   
> [code2=powershell]$vms = "testvm", "testvm1"  
>   
> $myArray = @()  
> $object = new-object Object  
>    
> foreach ($vm in $vms){  
>    
> $object | add-member NoteProperty "DB Name" $vm  
> $object | add-member NoteProperty "DB Name" (get-datastore -vm $vm).name  
> $object | add-member NoteProperty "DB Size" (get-datastore -vm $vm).CapacityGB  
> $object | add-member NoteProperty "OS Name" (get-vmguest -vm $vm).OSFullName  
>   
> $myArray += $obj  
> $object = new-object Object   
> }[/code2]  
>   
> $myArray now contain properties from 2 vms and i can you Format-table

by megamorf at 2012-12-05 08:20:25

> `$vms = "testvm", "testvm1" $myArray = @() foreach ($vm in $vms){ $name = $vm.name $dbname = (get-datastore -vm $vm).name $dbsize = (get-datastore -vm $vm).CapacityGB $vmos = (get-vmguest -vm $vm).OSFullName $myArray += "" | Select-Object @{n="name";e={$name}},@{n="dbName";e={$dbname}},@{n="dbSize";e={$dbsize}},@{n="OS";e={$vmos}}}$myArray | format-table -autosize`  
>   
> What are you trying to achieve? You collect objects in an array and can decide how you want to output it by using the format-Cmdlets like format-list, format-table, etc (get-command -verb format)

by andrewv88 at 2012-12-05 12:10:25

> **megamorf** thanks !

by megamorf at 2012-12-05 12:31:49

> [quote="andrewv88"] **megamorf** thanks ![/quote]  
>   
> Well, I quickly wrote that on my way home. I’d do it with a hashtable like this (I like to have clean code so that my colleagues can understand what’s going on):  
>   
> `$vms = "testvm", "testvm1" $myArray = @() foreach ($vm in $vms){ $properties = @{$name = $vm.name; $dbname = (get-datastore -vm $vm).name; $dbsize = (get-datastore -vm $vm).CapacityGB; $vmos = (get-vmguest -vm $vm).OSFullName } $myArray += New-Object â€“TypeName PSObject â€“Prop $properties}$myArray | format-table -autosize`

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:57pm UTC](https://forums.powershell.org/t/create-custom-table-for-showing-results/1028/2 "2024-05-16T20:57:56Z")

</div>

Closed on May 16, 2024, 8:57pm UTC


