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.
Setup
If you have not already done so, click open Windows PowerShell ISE.
Step one.
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:
Pre $MyString = " PowerShell "$MyDouble = 2.0 $outstring = $MyString + $MyDoublewrite-output $outstring Pre
You will not have any issues in the combination. That is because $MyDouble was converted automatically to a string by the system.
Step two.
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.
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.
Step 3.
You can cast a variable using the â€"as operator. Simply replace the outstring code with the following:
This is actually the preferred method for casting values, however you can shorthand it by simply prefixing the value or variable with the data type, as previously demonstrated in step 2.
If you want to see what occurs when you explicitly cast a $MyDouble to a string before combining with $MyString, run the following code:
Example
$MyString = " PowerShell "$MyDouble = 2.0
$outstring = $MyString + $MyDoublewrite-output $outstring
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.
A Few Last Word
Casting values will definitely be used throughout the tutorials on the site. We discuss the various data types in a future tutorial. Join us next time for additional Windows PowerShell tutorials! Till then