Accessing properties

Properties of an object in PowerShell may be accessed by writing the property name after a period. For example, the Name property of the current PowerShell process may be accessed by using the following code:

$process = Get-Process -Id $PID 
$process.Name 

PowerShell also allows us to access these properties by enclosing a command in parentheses:

(Get-Process -Id $PID).Name 

Properties of an object are objects themselves. For example, the StartTime property of a process is a DateTime object. We may access the DayOfWeek property by using the following code:

$process = Get-Process -Id $PID 
$process.StartTime.DayOfWeek 

The variable assignment step may be skipped if parentheses are used:

(Get-Process -Id $PID).StartTime.DayOfWeek 

If a property name has a space, it may be accessed using a number of different notation styles. For example, a property named 'Some Name' may be accessed by quoting the name or enclosing the name in curly braces:

$object = [PSCustomObject]@{ 'Some Name' = 'Value' } 
$object."Some Name" 
$object.'Some Name' 
$object.{Some Name} 

A variable may also be used to describe a property name:

PS> $object = [PSCustomObject]@{ 'Some Name' = 'Value' }
PS> $propertyName = 'Some Name'
PS> $object.$propertyName
Value
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.19.63.106