Advanced Windows PowerShell Scripting Video Training

Advanced Windows PowerShell Scripting Video Training
Advanced Windows PowerShell Scripting Video Training

Friday, December 2, 2016

Accessing the Values of Properties

We need to be able to access the values of properties to make decisions.  There are multiple ways to do this.
Using Select-Object
PS C:\> Get-Volume | Select-Object -ExpandProperty Size
471855104
366997504
255919648768
255919648768
515895296
116280782848
9694433280

Using dot notation
PS C:\> (Get-Volume).Size
471855104
366997504
255919648768
255919648768
515895296
116280782848
9694433280

Using a variable
PS C:\> $Data = Get-Volume

PS C:\> $Data.Size
471855104
366997504
255919648768
255919648768
515895296
116280782848
9694433280

Using array syntax
PS C:\> $Data[0].Size
471855104

You have access to all information in a property.  Just choose the method that works best for you and your situation. The important thing to remember is that Get-Member shows you the correct property name to use.  Some formatted output changes the property names.
PS C:\> Get-Process | Select-Object -First 1

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id  SI ProcessName                
-------  ------    -----      ----- -----   ------     --  -- -----------                
    106      10     1564       3640    91    73.70   7080   1 ApMsgFwd 

Notice the name of the column CPU(S).  If you try to use it, you will not get good results.
PS C:\> Get-Process | Select-Object -Property "CPU(S)"

CPU(S)
------
      

Using Get-Member you will discover that the correct name of the property is actually CPU.  Now you get the correct results.
PS C:\> Get-Process | Select-Object -Property "CPU"

        CPU
        ---
  73.828125
   46.34375
   52.03125



No comments: