Advanced Windows PowerShell Scripting Video Training

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

Wednesday, November 18, 2015

Keeping a Numeric Data Type a Number after Formatting

I’m up in Toronto, Canada tonight.  I’m delivering a custom 3 day PowerShell class.  Yes, 3 days.  Not a lot of time.  For that reason, no question is off the table.  Normally when a question is asked that is covered later in the course, I ask for them to hold off so we can build our knowledge base up a bit more so we can answer the question in more depth.  Did I mention that we only had 3 days!!!!

So we had a question after I used Get-Volume.  It turns out that we have some PowerShell V4 users on Windows 7 in the audience.  Get-Volume is from the Storage module that comes from the Windows 8 operating system, they could not use it.  Since we were about to cover calculated properties, I went ahead and used that as an opportunity to take a question and make a lesson out of it.  Here is our code.

1

2

3

4

GWMI Win32_Volume |

Select -Property DriveLetter,

    @{N = "SizeGB";E = {$_.Capacity/1gb}},

    @{N = "FreeSpaceSizeGB"; E = {$_.FreeSpace/1gb}} 

Yes I know, I used short hand.  A rarity but they wanted to see it as compressed as possible.  Nothing note worthy here.  The out bothered them a bit.

DriveLetter                                                SizeGB                 FreeSpaceSizeGB

-----------                                                ------                 ---------------

                                                0.341793060302734              0.0870437622070313

E:                                               19.9970664978027                12.6184005737305

C:                                               9.65624618530273                1.36867141723633

D:                                               3.98966598510742                               0

 

 

They asked for 2 decimal places on the custom properties.  So, I showed them a few ways to do this.

1

2

3

4

GWMI Win32_Volume |

Select -Property DriveLetter,

    @{N = "SizeGB";E = {($_.Capacity/1gb).ToString("#.##")}},

    @{N = "FreeSpaceSizeGB"; E = {"{0:N2}" -f ($_.FreeSpace/1gb)}}

And here is the output:

DriveLetter                      SizeGB                           FreeSpaceSizeGB               

-----------                      ------                           ---------------               

                                 .34                              0.09                          

E:                               20                               12.62                         

C:                               9.66                             1.37                          

D:                               3.99                             0.00                          

 

 

Looks good.  The problem is that both calculated properties are now System.String objects, not System.Double which is what they originally were.  OK, problem.  Fortunately we have the –as operator.  The –as operator will convert from one data type to another if it is possible.  Since we had a string that was nothing but numbers, it was possible.  Here is the original Get-Member information without the use of the –as operator.

TypeName: Selected.System.Management.ManagementObject

 

Name            MemberType   Definition                        

----            ----------   ----------                        

Equals          Method       bool Equals(System.Object obj)    

GetHashCode     Method       int GetHashCode()                 

GetType         Method       type GetType()                    

ToString        Method       string ToString()                 

DriveLetter     NoteProperty string DriveLetter=D:             

FreeSpaceSizeGB NoteProperty System.String FreeSpaceSizeGB=68.88

SizeGB          NoteProperty System.String SizeGB=238.34       

 

 

I highlighted in yellow the problem. Here is the code using the –as- operator.

 

1

2

3

4

GWMI Win32_Volume |

Select -Property DriveLetter,

    @{N = "SizeGB";E = {($_.Capacity/1gb).ToString("#.##") -as [Double]}},

    @{N = "FreeSpaceSizeGB"; E = {"{0:N2}" -f ($_.FreeSpace/1gb) -as [Double]}} 

 

Finally, here is our object after being piped to Get-Member.  Again, I highlighted in yellow what you should be interested in,

    TypeName: Selected.System.Management.ManagementObject

 

Name            MemberType   Definition                        

----            ----------   ----------                        

Equals          Method       bool Equals(System.Object obj)    

GetHashCode     Method       int GetHashCode()                 

GetType         Method       type GetType()                    

ToString        Method       string ToString()                 

DriveLetter     NoteProperty string DriveLetter=D:             

FreeSpaceSizeGB NoteProperty System.Double FreeSpaceSizeGB=68.88

SizeGB          NoteProperty System.Double SizeGB=238.34 

 

I have always stressed in my PowerShell classes the importance of knowing what your object is doing in the pipeline.  Get-Member is a great way to not only know what properties and methods you have at your disposal, but also if any of them changed in a way that you do not want them to.

 

 

No comments: