PowerShell Tips: Casting Your Data Types

I recently wrote a blog about importing data from a CSV file to use in PowerShell scripting. I added a little note at the bottom of that blog noting that each item’s value that is imported from a CSV file will be a string data type. Well, what if the data that you imported from the CSV wasn’t originally a string? How can you cast your data back to the type that it was originally?

[string]$iAmAnInt = “5”

[int]$iAmAnInt = $iAmAnInt


PowerShell Typecast 1

Okay, well that was easy. I guess we’re done here, right? Not quite.

Let’s take a closer look at what just occurred there. First, I took a variable of type string named “$iAmAnInt.” Then I set a variable of type int named “$iAmAnInt” to the previously declared variable. And now when I run the GetType() method on $iAmAnInt, it’ll show that it is an integer.

NOTE: Casting any type of number (integer, double, etc.) into a string will always work, but the inverse is not true! When attempting to cast strings into numbers you will need to be sure that the strings actually represent a number or else the casting won’t work! For example, if you try to cast a string that is obviously not a number to an int or double data type, then you’ll get an error stating that the input string was not in a correct format:

[string]$iAmAString = “5asdf”

$answer = [int]$iAmAString

PowerShell Typecast 2

So let’s look at how we can test that a string will work as a number before attempting to cast it into a number data type:

[string]$thisWasAString = “89abc”


if( ($thisWasAString -as [double]) -ne $null )


    [double]$thisWasAString = $thisWasAString




PowerShell Typecast 3

Here is the key part of the code above:

if( ($thisWasAString -as [double]) -ne $null )

I used the “-as” operator to attempt to evaluate $thisWasAString as a double. If this can work, then it won’t return $null. So we want to make sure that the cast won’t return $null, and if it doesn’t then we can cast away! So I’ll change $thisWasAString back to an acceptable number value and test this again:

PowerShell Typecast 4

While we’re on the subject, it’s also possible to cast a variable on the fly without permanently changing the variable’s data type. Check this out:

[double]$aNum = 5.02

[int]$anInt = 10

[int]$newInt = ($aNum –as [int]) + $anInt

($aNum –as [int])


write-host “`$aNum =” $aNum.GetType().FullName

PowerShell Typecast 5

On line 3, set $newInt by taking the $aNum variable as an int and adding it to the $anInt variable. This is where the value of 15 comes from. The other thing to keep in mind is, after all is said and done, on line 6 we can see that $aNum is still a double.

Whenever you don’t explicitly cast a data type for a variable then PowerShell will do its best to figure out the data type for the variable you’re intending to use in your scripts. Here are a few examples where I did not specify a data type for some variables, so PowerShell determined the variable’s data type based on the value:

$thisIsAnInt = 7

$thisIsADouble = 6.789

$thisIsABoolean = $false

$thisIsAString = “This is a string”






PowerShell Typecast 6

Now let’s take a look at the Export-CSV and Import-CSV cmdlets to see why the issue of data types may come up when using CSVs. First, I’ll create a simple PSObject using the variables I created above and then export it to a CSV:

$csvPath = “C:\filelocation\exportedFile.csv”

$anObject = New-Object psobject -Property @{

int = $thisIsAnInt


boolean = $thisIsABoolean

string = $thisIsAString



Next, I’ll just confirm that these variables are the data type I expect:

PowerShell Typecast 7

Let’s export the object to a CSV:

$anObject | Export-Csv -Path $csvPath –Append

After running the command above, I verified the file was created:

PowerShell Typecast 8

Let’s re-import this object:

$importedObject = import-csv $csvPath

And now let’s check to see what data types each member of the new object is:

ForEach ($item in $importedObject)







PowerShell Typecast 9

We re-imported the object, but all the values are strings! (Of course this should not come as a surprise since we already knew this would happen.) So what would happen if I just tried to add the integer and the double without casting them back to their original data types? Well, the same thing that would normally happen when adding strings together…

ForEach ($item in $importedObject)


    $sum = ($item.double) + ($item.int)

    write-host ($item.double)

    write-host ($item.int)

    write-host $sum


PowerShell Typecast 10

It just appended one string to the other string. Now let’s try adding them after casting them both back to their original data types:

ForEach ($item in $importedObject)


    if( ($item.double -as [double]) -ne $null)


        [double]$item.double = $item.double


    if( ($item.int -as [int]) -ne $null)


        [int]$item.int = $item.int



    $sum = ($item.double) + ($item.int)

    write-host “`$item.double is type:” ($item.double).GetType().FullName

    write-host “`$item.int is type:” ($item.int).GetType().FullName

    write-host “`$sum =” $sum


 PowerShell Typecast 11

The example above actually casts the $item.double and $item.int variables to double and integer data types. If you don’t want to bother with actually changing the data types of those variables, then we could just use the “-as” operator like we did earlier:

$importedObject = import-csv $csvPath


ForEach ($item in $importedObject)


    $sum = ($item.double -as [double]) + ($item.int -as [int])

    write-host “`$item.double is type:” ($item.double).GetType().FullName

    write-host “`$item.int is type:” ($item.int).GetType().FullName

    write-host “`$sum =” $sum


PowerShell Typecast 12

There were some key differences here.

First, since we didn’t change the data types of $item.double and $item.int. They are still strings when everything is all said and done! The key here was using the “-as” operator to treat those variables as the intended data types only when setting the $sum variable.

At the end of the day, I generally don’t have to worry about type casting too often with PowerShell. It generally does a good job of handling things behind the scenes. However, no computer can fully read our minds (yet), so sometimes we need to know how to tell these machines exactly what we mean. And it’s during those times that it’s good to know how to type cast your PowerShell data.

Thanks for reading! Until next time…

2015-06-15T09:00:39+00:00 June 15th, 2015|

Leave A Comment