function to return format as table

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 ? :slight_smile:

Thanks

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

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.

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

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

Hi Richard,

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

Thanks

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

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.
https://blogs.msdn.microsoft.com/powershell/2006/06/23/leveraging-windows-powershell-type-extensions-to-get-documentation/

I found just this and will try it out

https://learn-powershell.net/2013/08/03/quick-hits-set-the-default-property-display-in-powershell-on-custom-objects/

Ah great! Looks promising. Keep us updated!

Awesome! thanks for sharing - I had no idea