Code
Set-StrictMode -Version Latest; function listObjectProperties($object) { [int] $iPropertyIndex = 0; #prepare formatting $strFormat = "{0}) Name :- {1} - Value :- {2}" #Iterate Object Properties Foreach ($objProperty in $objProperties) { # increment property counter $iPropertyIndex = $iPropertyIndex + 1; $objPropertyName = $objProperty.Name; #place variable name in single quotes to ensure that #PowerShell does not evaluate\substite value $objPropertyNameFull = '$object' + '.' + $objPropertyName #prepare to use variable substitution # Invoke-Expression # http://technet.microsoft.com/en-us/library/dd347550.aspx # dadeniji 2018-01-22 #$objPropertyValue = invoke-expression $objPropertyNameFull; $objPropertyValue = "$($objProperty.Value)" #format data $strLog = [String]::Format( $strFormat , $iPropertyIndex , $objPropertyName , $objPropertyValue ); # display data $strLog; } } #PowerShell 2.0 Commands #http://ss64.com/ps/ Clear-Host; # instantiate object [Datetime] $object = Get-Date; if (!$object) { "Object is null (empty)" return } # Keith Hill - Get Type name # http://rkeithhill.wordpress.com/2007/10/28/powershell-quicktip-using-pstypenames-to-see-# # the-typename-and-inheritance-chain/ $strLog = "Type name is " + $object.GetType().Fullname; $strLog # get Object Properties $objProperties = $object.PsObject.Properties; if (!$objProperties) { "Object has no properties" return } #list properties listObjectProperties( $object)
Source Code
Github
DanielAdeniji/PowerShellDynamicallyListProperties
Link
[…] Btw, here is the original post. […]