DevOps | Scripts | Automation

Powershell

Variables and Datatypes in PowerShell

Variables in PowerShell is to store values like any other programming languages. It can store a single value or multiple values. Assume it is like a container is initially empty and we can put anything inside it.

So once the value is stored inside the container, we can use it, change it and delete it. There are a few ways to create a PowerShell variable. The values we store in the container becomes their data types. About datatypes, we learn in the article only.

Ways to create PowerShell Variable

First Method:

Variables in the PowerShell can be identified with $ signature followed by the name of the variable. For example,

Here, we have created a variable named $service by just declaring it.

Second Method:

Another method to create a variable with the use of cmdlet New-Variable. In the below example, variable name $processes will be created without any value or datatype.

We haven’t assigned any value in the above methods, so both variable values will be Null initially.

Assigning Multiple Values:

PowerShell allows assigning the multiple values to the multiple variables together. For example,

$a,$b,$c = 'Cat','Dog','Tiger'

In the above example, values are assigned to the variables respectively.

Variable name naming conventions:

There is no specific naming convention for the variable, you can use any name but just need to avoid predefined system variable names, preference variable name, or Automatic variable name. Also, we need to keep in mind not to start a variable name with Wildcard characters (%, &,*, etc) or space or these characters should not contain in a variable name.

List of Automatic and Preference variables.

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_preference_variables?view=powershell-7

Maximum Variable length:

The maximum length of the variable supported by PowerShell is 2147483647. Generally, no one uses this much bigger length of the variable and it doesn’t make any sense. However, if you want to check the variable name length then use the below code.

(New-Object Text.StringBuilder "test").MaxCapacity

One more thing, you can’t use Space and avoid special characters in the name. Generally, when you write a program you should give the identical names of the variables. For example, if the variable contains the services information then it should be something like $service, if it talks about processes then it should be $process. It is not a PowerShell standard but another programmer who is reading your program should know what the variable holds.

Checking Variable Datatype

Coming back to the out $service variable that we have declared, it doesn’t contain any value. So when you print the variable by its name the value would be nothing. To check the datatype of any variable, we need to use the GetType() method.

In the above example, there is no value assigned to a $variable hence there is an error. Now if we assign value to the variable then it becomes that datatype. For example, If we assign an integer value, it becomes the integer datatype.

If we assign String value or float value it becomes that datatype.

You can also confirm the datatype of the variable by using -Is the operator also called the comparison operator.

10 -is [Int]
Output: True

10.4 -is [Double]
Output: True

10 -is [String]
Output: False

"Hello" -is [String]
Output: True

(Get-Date) -is [DateTime]
Output: True

"10/10/2018" -is [DateTime]
Output: False
List of commonly used Datatypes in PowerShell
  • [Int32] – 32 bit Integer datatype
  • [Long] – 64 bit Integer value.
  • [String] – String Datatype
  • [Single] – float point datatype – 32 bit
  • [Double] – Float point datatype – 64 bit
  • [DateTime] – DateTime datatype
  • [char] – A unicode 16 bit character
  • [Decimal ] – 128 bit decimal value
  • [bool] – boolean (True or False) value
  • [XML] – XML object type
  • [Hashtable] – Hashtable values
  • [array] – Array objects

Explicitly assigning Datatype to the variable.

You can assign datatypes to the variables explicitly. This technique is called the type conversion and it is commonly used when we work with the functions and when we declare parameters inside the function. It is always a good practice first to declare the variable with datatype so when another person reads the script, he can get an idea about the use of the variable by datatype and the proper name of the variable. For example,

[Int]$CPUs = 4

The above example says the name of the variable is $CPUs and it should contain only an Integer. If we assign any another datatype value then it shouldn’t work and you will get the runtime exception error as shown below.