Some objects or functions expect a particular data type and sometimes, you might just need a particular output that requires a different data type or the combination of two different data types. In order to do so, you will need to convert the one data type to the other, otherwise known as casting. In this tutorial, you will learn how to cast values or change/combine their data type.
Click open Windows PowerShell ISE.
Follow Below steps
Casting refers to the process of changing a variable’s data type from one value to another. Windows PowerShell was actually prepared to take a few steps to help you make this process work. It compares the two (or more) data types being combined, as long as they match, it will continue on. If they are different, it will attempt to cast, or convert the values to a common data type. It will attempt to look for the data type that can store the values being converted without losing its original value and will attempt to convert the value to this data type.
This feature is very valuable, since it prevents you from losing any data. Losing data is simply unacceptable in development. Once all the data types are the same, it combines the values and returns the results. Copy and paste the following script:
Example
$MyVarString = "XYZ"
$MyVarDouble = 2.0
$outvarstring = $MyVarDouble + $MyVarString
write-output $outvarstring
You will not have any issues in the combination. That is because $MyVarDouble was converted automatically to a string by the system.
If we were to switch it around, we may encounter some problems, as strings cannot be converted to certain data types such as int and double. Exchange the outstring for the following and run:
As you see, the system responded with an error, saying the conversion cannot be made.
Example
Cannot convert value "XYZ" to type "System.Double". Error: "Input string was not in a correct format."
At line:3 char:1
+ $outvarstring = $MyVarDouble + $MyVarString
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastFromStringToDoubleOrSingle
Now, you can explicitly cast the double to a string, essentially you are controlling the casting at this point whether than PowerShell doing it automatically. This is done by exchanging the outstring for the following:
Now when you run the code, it will do the conversion, but the string will read backwards. This was merely done as a demonstration as to what is acceptable and what is not in PowerShell.
You can cast a variable using the as a operator. Simply replace the outstring code with the following:
Example
Trace-Command -Name TypeConversion -pshost {[string]$MyVarDouble + $MyVarString}
Example
PS C:\WINDOWS\system32> Trace-Command -Name TypeConversion -pshost {[string]$MyVarDouble + $MyVarString}
DEBUG: TypeConversion Information: 0 : Converting numeric to string.
2XYZ
Getting acquainted with the PSH data types is actually recommended at this point, since there are so many ways in which conversion is applied in scripting. The Trace-Command can also be used to debug other tasks, for more information, run Get-Help on Type Conversion, PowerShell breaks it down for you.