# function to return format as table

**URL:** <https://forums.powershell.org/t/function-to-return-format-as-table/6277>\
**Category:** PowerShell Help\
**Created:** [April 21, 2016, 4:40pm UTC](https://forums.powershell.org/t/function-to-return-format-as-table/6277 "2016-04-21T16:40:13Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![skest3qc](https://avatars.discourse-cdn.com/v4/letter/s/f9ae1b/32.png) [@skest3qc](https://forums.powershell.org/u/skest3qc)\
**Post date:** [April 21, 2016, 4:40pm UTC](https://forums.powershell.org/t/function-to-return-format-as-table/6277/1 "2016-04-21T16:40:13Z")

</div>

Hi,  
i have a small function  
`
function Get-FreeDiskSpace
{
$Items = Get-CimInstance -ClassName Win32_LogicalDisk`

`
```
	$RetVal = foreach ($Item in $Items)
	{
		[pscustomobject]@{
			DeviceID = $Item.DeviceID
			DriveType = $Item.DriveType
			VolumeName = $Item.VolumeName
			ProviderName = $Item.ProviderName
			Size = "{0,7:#.00} GB" -f ($Item.Size/1GB)
			FreeSpace = "{0,7:#.00} GB" -f ($Item.FreeSpace/1GB)
			PercentFree = @(if ($Item.Size -gt 0) { "$([Math]::round((($Item.FreeSpace/$Item.Size) * 100))) %" } else { "0 %" })[0]
			SystemName = $Item.SystemName
		}
	}

    $RetVal

```
`

`}
`

which returns values like this:  
DeviceID : C:  
DriveType : 3  
VolumeName : Windows  
ProviderName :  
Size : 118,63 GB  
FreeSpace : 44,54 GB  
PercentFree : 38 %  
SystemName : mimi

DeviceID : D:  
DriveType : 3  
VolumeName : Volume  
ProviderName :  
Size : 1863,01 GB  
FreeSpace : 1344,11 GB  
PercentFree : 72 %  
SystemName : mimi

I want the function to return values like Format-Table. But i still want to have access to the objects in the pipeline.

What am i missing here ? 🙂

Thanks

---

<div class="post-metadata">

**Author:** ![rdtechie](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rdtechie/32/273_2.png) [@rdtechie](https://forums.powershell.org/u/rdtechie)\
**Post date:** [April 21, 2016, 7:21pm UTC](https://forums.powershell.org/t/function-to-return-format-as-table/6277/2 "2016-04-21T19:21:55Z")

</div>

I modified your code, and took out the variables. Try this:

```
function Get-FreeDiskSpace
{
  foreach ($Item in Get-CimInstance -ClassName Win32_LogicalDisk)
  {
    [pscustomobject]@{
      DeviceID = $Item.DeviceID
      DriveType = $Item.DriveType
      VolumeName = $Item.VolumeName
      ProviderName = $Item.ProviderName
      Size = '{0,7:#.00} GB' -f ($Item.Size/1GB)
      FreeSpace = '{0,7:#.00} GB' -f ($Item.FreeSpace/1GB)
      PercentFree = @(if ($Item.Size -gt 0) 
        {
          "$([Math]::round((($Item.FreeSpace/$Item.Size) * 100))) %" 
        }
        else 
        {
          '0 %' 
        }
      )[0]
      SystemName = $Item.SystemName
    }
  }
}
```

Works perfectly when doing something like this then:

```
Get-FreeDiskSpace | Format-Table
```

---

<div class="post-metadata">

**Author:** ![liam-kemp](https://avatars.discourse-cdn.com/v4/letter/l/48db29/32.png) [@liam-kemp](https://forums.powershell.org/u/liam-kemp)\
**Post date:** [April 21, 2016, 8:57pm UTC](https://forums.powershell.org/t/function-to-return-format-as-table/6277/3 "2016-04-21T20:57:36Z")

</div>

Hi Selko,  
I got bogged down trying to do exactly the same thing not so long ago, until i got some great advice here. Don’t forget the primary purpose of a function - To collect and process the information. The formatting comes later. Specifically, when the function is run. So don’t worry about how the function is actually outputting the data, because this can be changed when the function is run by piping it to format-table.

---

<div class="post-metadata">

**Author:** ![skest3qc](https://avatars.discourse-cdn.com/v4/letter/s/f9ae1b/32.png) [@skest3qc](https://forums.powershell.org/u/skest3qc)\
**Post date:** [April 21, 2016, 10:06pm UTC](https://forums.powershell.org/t/function-to-return-format-as-table/6277/4 "2016-04-21T22:06:22Z")

</div>

Hi,

@Richard: Thanks but my original code works the same like your code.  
@Liam: I understand that formatting should happen outside the function but i would like to change the default formatting (to Format-Table) like Get-Process it does.

Maybe there is someone which has solved this…

Thanks

---

<div class="post-metadata">

**Author:** ![rdtechie](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rdtechie/32/273_2.png) [@rdtechie](https://forums.powershell.org/u/rdtechie)\
**Post date:** [April 21, 2016, 10:18pm UTC](https://forums.powershell.org/t/function-to-return-format-as-table/6277/5 "2016-04-21T22:18:57Z")

</div>

Selko, your question was not clear so I had to guess. My code is simpler and conforms to some community best practices. Now that i understand your question, look into this. It will answer what you need. [https://technet.microsoft.com/en-us/library/hh847831.aspx](https://technet.microsoft.com/en-us/library/hh847831.aspx)

---

<div class="post-metadata">

**Author:** ![skest3qc](https://avatars.discourse-cdn.com/v4/letter/s/f9ae1b/32.png) [@skest3qc](https://forums.powershell.org/u/skest3qc)\
**Post date:** [April 21, 2016, 10:34pm UTC](https://forums.powershell.org/t/function-to-return-format-as-table/6277/6 "2016-04-21T22:34:50Z")

</div>

Hi Richard,

thanks for the link. I hoped that it is possible somehow without creating extra files.

Thanks

---

<div class="post-metadata">

**Author:** ![rdtechie](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rdtechie/32/273_2.png) [@rdtechie](https://forums.powershell.org/u/rdtechie)\
**Post date:** [April 21, 2016, 10:42pm UTC](https://forums.powershell.org/t/function-to-return-format-as-table/6277/7 "2016-04-21T22:42:44Z")

</div>

As far as I know you have to use custom formatting files.

---

<div class="post-metadata">

**Author:** ![anthony-stringer](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/anthony-stringer/32/593_2.png) [@anthony-stringer](https://forums.powershell.org/u/anthony-stringer)\
**Post date:** [April 21, 2016, 10:45pm UTC](https://forums.powershell.org/t/function-to-return-format-as-table/6277/8 "2016-04-21T22:45:03Z")

</div>

you would either have to do this outside the function like this

```
# save to variable
$freediskspace = Get-FreeDiskSpace

# format for viewing in console
$freediskspace | ft -a

# do something else with it
$freediskspace | epcsv c:\temp\freediskspace.csv -not
```

or use an external file. it is not as difficult as it at first seems.

> **[Leveraging Windows PowerShell Type Extensions to get documentation](https://devblogs.microsoft.com/powershell/leveraging-windows-powershell-type-extensions-to-get-documentation/)**
>
> After just a little use of Windows PowerShell, you quickly learn that you need to pipe objects in the GET-MEMBER utility to understand the capabilities of that object.  Get-Member reflects against the object and shows you all of its methods and...

---

<div class="post-metadata">

**Author:** ![skest3qc](https://avatars.discourse-cdn.com/v4/letter/s/f9ae1b/32.png) [@skest3qc](https://forums.powershell.org/u/skest3qc)\
**Post date:** [April 21, 2016, 10:48pm UTC](https://forums.powershell.org/t/function-to-return-format-as-table/6277/9 "2016-04-21T22:48:29Z")

</div>

I found just this and will try it out

> **[Quick Hits: Set the Default Property Display in PowerShell on Custom Objects](https://learn-powershell.net/2013/08/03/quick-hits-set-the-default-property-display-in-powershell-on-custom-objects/)**
>
> Whenever we write a function or a  script and choose what properties to display, we are usually greeted with the usual display of every single property that we used instead of what the the Pow…

---

<div class="post-metadata">

**Author:** ![rdtechie](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rdtechie/32/273_2.png) [@rdtechie](https://forums.powershell.org/u/rdtechie)\
**Post date:** [April 21, 2016, 10:52pm UTC](https://forums.powershell.org/t/function-to-return-format-as-table/6277/10 "2016-04-21T22:52:45Z")

</div>

Ah great! Looks promising. Keep us updated!

---

<div class="post-metadata">

**Author:** ![anthony-stringer](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/anthony-stringer/32/593_2.png) [@anthony-stringer](https://forums.powershell.org/u/anthony-stringer)\
**Post date:** [April 21, 2016, 11:08pm UTC](https://forums.powershell.org/t/function-to-return-format-as-table/6277/11 "2016-04-21T23:08:49Z")

</div>

Awesome! thanks for sharing - I had no idea

---

<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:41pm UTC](https://forums.powershell.org/t/function-to-return-format-as-table/6277/12 "2024-05-16T20:41:33Z")

</div>


