Skip to main content

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.

 

 

Comments

Popular posts from this blog

Adding a Comment to a GPO with PowerShell

As I'm writing this article, I'm also writing a customization for a PowerShell course I'm teaching next week in Phoenix.  This customization deals with Group Policy and PowerShell.  For those of you who attend my classes may already know this, but I sit their and try to ask the questions to myself that others may ask as I present the material.  I finished up my customization a few hours ago and then I realized that I did not add in how to put a comment on a GPO.  This is a feature that many Group Policy Administrators may not be aware of. This past summer I attended a presentation at TechEd on Group Policy.  One organization in the crowd had over 5,000 Group Policies.  In an environment like that, the comment section can be priceless.  I always like to write in the comment section why I created the policy so I know its purpose next week after I've completed 50 other tasks and can't remember what I did 5 minutes ago. In the Group Policy module for PowerShell V3, th

Return duplicate values from a collection with PowerShell

If you have a collection of objects and you want to remove any duplicate items, it is fairly simple. # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   # Remove the duplicate values. $Set1 | Select-Object -Unique 1 2 3 4 5 6 7 What if you want only the duplicate values and nothing else? # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   #Create a second collection with duplicate values removed. $Set2 = $Set1 | Select-Object -Unique   # Return only the duplicate values. ( Compare-Object -ReferenceObject $Set2 -DifferenceObject $Set1 ) . InputObject | Select-Object – Unique 1 2 This works with objects as well as numbers.  The first command creates a collection with 2 duplicates of both 1 and 2.   The second command creates another collection with the duplicates filtered out.  The Compare-Object cmdlet will first find items that are diffe

How to list all the AD LDS instances on a server

AD LDS allows you to provide directory services to applications that are free of the confines of Active Directory.  To list all the AD LDS instances on a server, follow this procedure: Log into the server in question Open a command prompt. Type dsdbutil and press Enter Type List Instances and press Enter . You will receive a list of the instance name, both the LDAP and SSL port numbers, the location of the database, and its status.