# PSObject, and splatting / formatting Cim\_LogicalDisk returns

**URL:** <https://forums.powershell.org/t/psobject-and-splatting-formatting-cim-logicaldisk-returns/11678>\
**Category:** PowerShell Help\
**Created:** [November 15, 2018, 8:08pm UTC](https://forums.powershell.org/t/psobject-and-splatting-formatting-cim-logicaldisk-returns/11678 "2018-11-15T20:08:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sdolcourt](https://avatars.discourse-cdn.com/v4/letter/s/d2c977/32.png) [@sdolcourt](https://forums.powershell.org/u/sdolcourt)\
**Post date:** [November 15, 2018, 8:08pm UTC](https://forums.powershell.org/t/psobject-and-splatting-formatting-cim-logicaldisk-returns/11678/1 "2018-11-15T20:08:50Z")

</div>

Hi, newbie to the forum.

Given the classic Get-DiskInfo type of POSH query, how would I display the results in a table, if splatting properties to a PSObject?

If I ship this across to a remote server:

$disk = Get-CimInstance -Class CIM\_LogicalDisk -CimSession $session | where drivetype -like 3

The return is nominally:

```
DeviceID DriveType VolumeName Size FreeSpace 
-------- --------- ---------- ---- -------- 
C: 3 Drive C 498892533760 85959804928 
E: 3 Drive E 24000140926976 14336094912512
```
If I create a hash-table and a new PSObject

$property = @{

Name = $disk.name

Label = $disk.volumeName

Size = $disk.size

Freespace = $disk.freespace

}

$object = New-Object PSObject -property $property

The display is:

```
PS C:\powershell> $object

Label Name Size Freespace

* * *

{Drive C, Drive E} {C:, E:} {498892533760, 24000140926976} {385959804928, 14336094912512}
```

1) I'd like to get this to format as one might see in Format-Table, that C and E are on separate lines.
1. How would I format the Size and Freespace in GB (similarly to “{0:N2}”), where I get 2 decimal places?

Many thanks,

Seth

---

<div class="post-metadata">

**Author:** ![ta11ow](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ta11ow/32/131_2.png) [@ta11ow](https://forums.powershell.org/u/ta11ow)\
**Post date:** [November 15, 2018, 8:26pm UTC](https://forums.powershell.org/t/psobject-and-splatting-formatting-cim-logicaldisk-returns/11678/2 "2018-11-15T20:26:37Z")

</div>

The first part of your issue is that you have _two_ objects, not one, but the way you’re creating your final object means you’re squashing them into a single object. This is what causes the lines to be merged; if you go about it differently, this won’t happen.

The second part is a trickier question. The easy answer is with a calculated property from Select-Object, which modifies the data to display what you want. I’ll come back to the trickier answer in a moment.

> <https://gist.github.com/vexx32/b7361670e79d96a0ba09a2753151d65a>

As you can see, you have a few options! But the core of the matter is that you have a collection of objects, so you either need to continue along the pipeline to enumerate them, or opt to enumerate them yourself with `foreach` or `ForEach-Object` when building your result objects. Multiple lines in a table = multiple output objects.

The trickier answer I mentioned before is that you can keep your raw data in bytes if you wish and delegate the display to PS’s formatting subsystem. Doing so is more complicated than I can really cover in a short forum post. Kevin Marquette covers it partially in [one of his deep dive posts on PSCustomObject](https://kevinmarquette.github.io/2016-10-28-powershell-everything-you-wanted-to-know-about-pscustomobject/?utm_source=reddit&utm_medium=comment#converting-a-hashtable).

---

<div class="post-metadata">

**Author:** ![sdolcourt](https://avatars.discourse-cdn.com/v4/letter/s/d2c977/32.png) [@sdolcourt](https://forums.powershell.org/u/sdolcourt)\
**Post date:** [November 17, 2018, 2:11pm UTC](https://forums.powershell.org/t/psobject-and-splatting-formatting-cim-logicaldisk-returns/11678/3 "2018-11-17T14:11:21Z")

</div>

Thank you for the ideas, Joel! I’ll give Kevin’s presentation a good read.

Generally, I have a Franken-script, hacked from Internet ideas, that uses Select-Object and the formatting math. Personal challenge is to make it more elegant.

Cheers,

Seth

---

<div class="post-metadata">

**Author:** ![sdolcourt](https://avatars.discourse-cdn.com/v4/letter/s/d2c977/32.png) [@sdolcourt](https://forums.powershell.org/u/sdolcourt)\
**Post date:** [November 17, 2018, 4:16pm UTC](https://forums.powershell.org/t/psobject-and-splatting-formatting-cim-logicaldisk-returns/11678/4 "2018-11-17T16:16:33Z")

</div>

Hi, Joel,

I learned a lot from your code of building with a hash-table. Many thanks for your explanation. From your example, I can see the flow through the pipeline.

What I didn’t catch before was using “foreach-object”. I’ve not previously used this cmdlet, so I had to overcome a reading bias that it was the classic foreach. Nope, two different things.

> **[ForEach-Object (Microsoft.PowerShell.Core) - PowerShell](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/foreach-object?view=powershell-7.2&viewFallbackFrom=powershell-6)**
>
> The ForEach-Object cmdlet performs an operation on each item in a collection of input objects. The input objects can be piped to the cmdlet or specified using the InputObject parameter. Starting in Windows PowerShell 3.0, there are two different ways...

I played with a few permutations of trying to get a pre-made PSObject fed into the script block using -ArgumentList, perhaps the way Invoke-Command can be fed a variable that exists locally

```
-Scriptblock {param($some_param) Do-This -parameter $some_param} -ArgumentList $some_param
```

Fun to try, just to see. But, ForEach-Object doesn’t seem work that way, learned that, moving on.

Cheers,

Seth

---

<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:33pm UTC](https://forums.powershell.org/t/psobject-and-splatting-formatting-cim-logicaldisk-returns/11678/5 "2024-05-16T20:33:39Z")

</div>

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


