Access Object Properties in Windows PowerShell Scripts

Q

How to access properties of ab object in Windows PowerShell scripts? I know the property name.

✍: FYIcenter.com

A

If you know the property name of an object, you can access the property value using the "." operator in Windows PowerShell scripts.

For example, if you run the following script:

$dt = Get-Date
$tod = $dt.TimeOfDay
$tod.TotalHours

PowerShell will do the following:

  • Run the "Get-Date" cmdlet, which returns a DateTime object that represents the current date and time.
  • Assign the DateTime object to variable $dt.
  • Get the TimeSpan object stored in the "TimeOfDay" property from $dt.
  • Assign the TimeSpan object to variable $tod.
  • Get the double value stored in the "TotalHours" property from $tod.
  • Display the double value, something like 22.4892652493611 (close to 11pm).

 

Introduction of Windows PowerShell Script

⇒⇒Windows PowerShell Tutorials

2016-11-02, 440👍, 0💬