Type Conversion
noun
say it: “TYPE kun-VUR-zhun” /ˈtaɪp kənˈvɜrʒən/
Turning a value into another type, such as text that looks like a number into a real number, by writing the type in square brackets in front of it.
What is type conversion in PowerShell?
Type conversion turns a value into another type, so text that looks like a number can be added up and sorted as one. In PowerShell you write the type in square brackets in front of the value, [decimal]'36.00' or [int]'3'; this is also called a cast. Values from Import-Csv are always text, so Sort-Object { [decimal]$_.Price } sorts prices by size, not alphabetically. A value that doesn't fit, such as '$36.00', stops with an error.
'36.00' + 1 # 36.001 (text)
[decimal]'36.00' + 1 # 37.00
In a sentence
- “Without a type conversion, PowerShell added 1 to the text 36.00 and got 36.001.”
- “A type conversion to decimal made the prices sort by size, so 129.99 came before 39.99.”
Level: Journeyman · More PowerShell terms at this level
Updated