Skip to content

Instantly share code, notes, and snippets.

@inammathe
Last active May 31, 2020 22:53
Show Gist options
  • Save inammathe/04d92717e17506c383d84f8d58590ed8 to your computer and use it in GitHub Desktop.
Save inammathe/04d92717e17506c383d84f8d58590ed8 to your computer and use it in GitHub Desktop.
Utility PoSH function that allows you to format a property of any object
<#
.SYNOPSIS
Utility function that allows you to format a property of any object
.DESCRIPTION
Invokes a script block against any number of object properties to manipulate the data
.EXAMPLE
PS C:\> Format-PropertyData -data ([PSCustomObject]@{FirstName = 'Evan'; Price = 100.0001}) -ColumnFormat @{'Price' = {[MATH]::Round($_)}}
Applies the math rounding function to all data in the 'Price' column
#>
function Format-PropertyData
{
param (
# The object you wish to manipulate
[Parameter(Mandatory)]
$Data,
# Hash table with the name of the property you wish to change and a script block to invoke against that data
[Parameter(Mandatory)]
$ColumnFormat
)
foreach ($format in $ColumnFormat.GetEnumerator())
{
foreach ($row in $data)
{
$row.($format.Key) = $row.($format.Key) | ForEach-Object { ($format.Value).invoke($_) }
}
}
return $data
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment