Hi, Damian Garbus here from Poshland Blog. Today I want to show 4 PowerShell variable types which should know all Powershell beginners. It’s very important if you want to automate your work with PowerShell scripts.

Why it’s important? – easy simple

In the first post about Powershell variable, I used the example of the box to which we can put in something or take something out. Here we can use the same box to show the example in this post. Imagine that you have in the box many paper documents. If you want to add to this box water, in the real world you won’t have clean paper and won’t have clean water. Paper documents and water will be mixed. You should use a different box for water. In Powershell is the same rule, except that Powershell doesn’t allow sum variable with different types. For example, Powershell doesn’t allow sum integer number and words.

How to declare type

Ok, so let’s check how to declare variable type. Look at the example.

[MyType]$VariableName

So if you declare variable to contain numbers you can’t add to this variable text.  But maybe first check the basics types of variable. 

4 Basics Powershell variable types 

[string] – characters, word or sentence. 

This is first of basics variable types. You can use it to put into variable characters, word or sentence. In the example below I show why “1 + 1” in the string variable is “11”.

[string]$MyVariable = "Powershell"
$MyVariable + " is cool"

[string]$MyVariable = 1
$MyVariable + 1
$MyVariable + 1 + 1 + 1
powershell variable types

[int] It’s integer number

Second type is integer which help you declare numbers. In the example I show that in this type is “1+1” is 2

[int]$Myvariable = 1
$MyVariable + 1
$MyVariable + 1 + 1 + 1
powershell variable types
[bool] Boolean True/False value. 

I think, this is the one of the most important types. Boolean tell’s us if something is true or false. Primary it’s useful when you write some conditions. In the example I use “-eq” operator which means “equal”. I sum 1 and 1 check if it’s 2 and next check if it’s 3. The result of these compares is Boolean type.

(1 + 1) -eq 2
(1 + 1) -eq 3
powershell variable types
[DateTime] Date and Time

And the last one type is DateTime. In programming language date and time has own format. It’s important if we want to do some operations on a date value. Check example.

[datetime]$MyVariable = Get-date
$MyVariable
$MyVariable.AddDays(1)
$MyVariable.AddDays(30)
$MyVariable.AddDays(-30)
powershell variable types

How to check variable type?

When we declare variable we know (and Powershell too) what type it is. But sometimes we want to check. To do this we can use one method. I know that I didn’t write about Powershell method yet but here it’s important so I show you.

[string]$MyVariable = "Powershell"
$Myvariable.GetType()
[datetime]$MyVariable = Get-date
$Myvariable.GetType()
powershell variable types

Conslusion

How did you see, Powershell variable types are very important. If you learn Powershell and write scripts you should know about this. I show you four most important for me. The variable types is more. You can check for example here. I hope that this post will help you to understand Powershell variable types. Please write in comment your basis variable types.

Get Access to Poshland Resources for FREE!!!

Signup now and receive an email once I publish new content.

I agree to have my personal information transfered to MailChimp ( more information )

I will never give away, trade or sell your email address. You can unsubscribe at any time.