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

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.

How to run GPResult on a remote client with PowerShell

In the past, to run the GPResult command, you would need to either physically visit this client, have the user do it, or use and RDP connection.  In all cases, this will disrupt the user.  First, you need PowerShell remoting enabled on the target machine.  You can do this via Group Policy . Open PowerShell and type this command. Invoke-Command –ScriptBlock {GPResult /r} –ComputerName <ComputerName> Replace <ComputerName> with the name of the target.  Remember, the target needs to be online and accessible to you.

Error icon when creating a GPO Preference drive map

You may not have an error at all.  Take a look at the drive mapping below. The red triangle is what threw us off.  It is not an error.  It is simply a color representation of the Replace option of the Action field in the properties of the drive mappings. Create action This give you a green triangle. The Create action creates a new mapped drive for users. Replace Action The Replace action gives you a red triangle.  This action will delete and recreate mapped drives for users. The net result of the Replace action is to overwrite all existing settings associated with the mapped drive. If the drive mapping does not exist, then the Replace action creates a new drive mapping. Update Action The Update action will have a yellow triangle. Update will modify settings of an existing mapped drive for users. This action differs from Replace in that it only updates settings defined within the preference item. All other settings remain as configured on the ma...