Author Posts

April 21, 2016 at 10:10 pm

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

April 22, 2016 at 12:51 am

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

April 22, 2016 at 2:27 am

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.

April 22, 2016 at 3:36 am

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

April 22, 2016 at 3:48 am

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

April 22, 2016 at 4:04 am

Hi Richard,

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

Thanks

April 22, 2016 at 4:12 am

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

April 22, 2016 at 4:15 am

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

April 22, 2016 at 4:22 am

Ah great! Looks promising. Keep us updated!

April 22, 2016 at 4:38 am

Awesome! thanks for sharing – I had no idea