Microsoft – PowerShell – List all the properties for an object…..

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

 

One thought on “Microsoft – PowerShell – List all the properties for an object…..

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s