Skip to content

Instantly share code, notes, and snippets.

@turboBasic
Created June 23, 2017 08:59
Show Gist options
  • Save turboBasic/bbc3cdf6937fce2d036665b55ec48477 to your computer and use it in GitHub Desktop.
Save turboBasic/bbc3cdf6937fce2d036665b55ec48477 to your computer and use it in GitHub Desktop.
ConvertTo-Hashtable: Powershell utility function which converts psCustomObject to HashTable for betters seeking and iteration
Function ConvertTo-Hashtable {
<#
.SYNOPSIS
Converts PsCustomObject type to Hashtable. Takes pipeline input and common arguments
.DESCRIPTION
Converts PsCustomObject type to Hashtable. Takes pipeline input, common arguments,
array arguments for bulk processing
#>
[CMDLETBINDING()] PARAM(
[PARAMETER( Position=0,
Mandatory,
ValueFromPipeline,
ValueFromPipelineByPropertyName )]
[ALIAS( 'CustomObject',
'psCustomObject',
'psObject' )]
[psCustomObject[]] $Object
)
BEGIN { }
PROCESS {
foreach ($_object in $Object) {
$output = @{ }
$_object | Get-Member -MemberType *Property | % {
$output.($_.name) = $_object.($_.name)
}
$output
}
}
END { }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment