Understanding the fundamental data types is crucial for leveraging the full potential of PowerShell. In this article, we will explore the three primary data types: String, Boolean, and Number.
Whether you’re new to PowerShell or looking to refresh your knowledge, this guide will provide a solid foundation for working with data in PowerShell.
Exploring String Interpolation and Manipulation
But there’s so much more to strings you might not know, so let’s dive in.
First, let’s create a string to play with.
$sentence = 'PowerShell r0cks but Python is a close second.'
We all know that PowerShell is the best language out there, and Python’s a close second. But what if we’re handing this code over to someone who disagrees? In that case, we need to make the string ‘Python’ a variable we can replace at any time.
You know how to create a variable, so let’s make one and call it $language
.
$language = 'Python'
The $language
variable currently has Python value. If we replace the static word Python in this string with $language
, PowerShell should interpret the variable and “expand” it in the string.
Let’s try it out.
$sentence = 'PowerShell r0cks but $language is a close second.'
Hmmm… the $language
variable is just that, a dollar sign ($), and the word language is not what we wanted. Why is that? The answer lies in the quotes.
We’ve been using single quotes all this time. When PowerShell sees a string in single quotes, it interprets everything inside literally and won’t expand any variables within. This feature is called string interpolation.
Let’s replace the single quotes with double quotes.
$sentence = "PowerShell r0cks but $language is a close second."
$sentence
Now, it’s expanding that $language
variable inside of the string.
You can also use string formatting if you don’t want to include the variable inside the string. Either way works. Strings are one of those simple objects in PowerShell, but you’ll use them constantly.
'PowerShell r0cks but {0} is a close second.' -f $language
Understanding Boolean Values and Conditional Logic
Let’s keep this train moving with the simplest object type, Boolean. A Boolean value will only ever be true or false.
Remember those automatic variables? Here are two of them.
$true
$false
The Boolean values True and False are variables you will assign to assign their value to other variables.
For example, you’ll need a flag at some point to check whether or not something is enabled.
You’ll often see variables that start with an is,
followed by a state like this.
$isEnabled = $true
$isEnabled
We now have a variable called $isEnabled
with a boolean value of $true
. This boolean value can only be $true
or $false
.
That’s it! Boolean values come in handy when using conditional logic.
Navigating Numerical Data and Casting
Next up, we have numbers. Typically, PowerShell takes care of most of the heavy lifting when representing different numbers, but you need to know about them.
Let’s start with integers. An integer is a whole negative or positive number from -2,147,483,647 to +2,147,483,647. Why 2,147,483,647? Don’t ask me. That’s just how it is.
Here, I’ll keep it simple and use 1
.
How do I know it’s now an integer? Because I assigned the value 1
without any quotes, so I know it’s not a string.
$num = 1
Let’s double-check. You can use the GetType()
method and look at the Name
property on any other object in PowerShell to get its type.
$num.GetType().name
Ah, Int32 is perfect, which is an integer.
This approach is often faster than using the Get-Member
cmdlet.
Now, try a decimal.
We’ve created a type of Double
. For now, don’t worry so much about the types. That rabbit hole is too deep; for now, know different numbers can be other types.
We may need a floating point, so “cast” the variable to a floating point.
$num = [float]$num
$num.GetType().name
Voila! PowerShell now shows the type as Single, which is a floating point.
This conversion is known as casting from one type to another and is a trick you’ll probably have to perform at some point.
Before we leave, let’s cover one more topic: arithmetic and types.
Let’s say we’ve created a few variables.
$one = '1'
$two = 2
Both values appear to be numbers, but one
is a string, and two
is an integer.
They both look like numbers, right? Let’s try to add them together.
$one + $two
Well, last time I checked, 1+2 wasn’t equal to 12. Why did that happen?
PowerShell concatenated or merged the two values because one was a string and couldn’t add a string or a number.
Let’s try that again, this time with two integers.
$one = 1
$two = 2
$one + $two
Now, that’s better. Since both variables are integers, PowerShell knows it can add them together.
Conclusion
Now that you’ve explored strings, Booleans, and numbers in PowerShell, you can handle data more effectively. This mastery will help you manipulate text, set up true/false flags, and confidently perform arithmetic.
You now understand how these data types work behind the scenes. Along with techniques like variable expansion and casting, you can now write robust and adaptable scripts.