Understanding PowerShell Variables and Datatypes

Variables are names of things that represent an object or value. Variables can be any name or combination of characters you want, so long as you follow the naming rules, otherwise known as naming conventions. In this tutorial you will learn which variable names are and are not acceptable and sorts of data types that exist in PowerShell.

Setup

If you have not already done so, click open Windows PowerShell. Remember, you must always run Windows PowerShell as the Administrator for best results and unlimited access.

Step one.

You may use a mixture of letters, numbers, as well as symbols and spaces. Some recommend against symbols and spaces for the sake of not making items look more cryptic than they should. Examples of acceptable variables are as follows, NOT a script or command:

Example

$MyVariable
$VarNum1
${Var With Spaces}
${[email protected]@Char}
    

Copy and Try it

The use of curly braces around a variable name gives you the flexibility to use almost any name you can come up with. And always with capitalization, Camel case notation is the best way to go! It is the practice of combining words without spaces by capitalizing the first letter of each word and each lowercase for subsequent letters in that word. You can explicitly define a variable using:

Example


Set-Variable

     

Copy and Try it

For help understanding the advanced properties of the command, run the following command:

Example

Get-Help Set-Variable
    

Copy and Try it

Step two.

There are variables of all sorts, such as numbers, characters, strings or objects (discussed in a different tutorial). These variations are known as data types. Variables that can have any data type are called variants and most scripting languages use them because of their flexibility.

PSH contains almost an infinite number of data types. Basic data types cover all of what are called primitive values. Primitive values are values you would typically expect to store as data and are the fundamental building blocks of the more complex data types. The basic data types and their definitions are listed in the table below:

Data Type

Definition

Boolean:

True or False Condition

Byte:

An 8-bit unsigned whole number from 0 to 255, such as 16.

Char:

A 16-bit unsigned whole number from 0 to 65,535. For example, 1,026.

Date:

A calendar date, such as January 1, 2009

Decimal:

A 128-bit decimal value, such as 3.14159

Double:

A double-precision 64-bit floating point number. In effect, this is another kind of decimal value but has narrower range of values than a decimal.

Integer:

A 32-bit signed whole number from -2,147,483,648 to 2,147,483,647, such as 152 or -1839.

Long:

A 64-bit signed whole number. This is like an integer but holds more values, such as 9,233,372,036,854,775,807.

Object:

Short

A 16-bit unsigned whole number. This is like an integer but holds far fewer values. It can only hold values from -32,768 to 32,767.

Single:

A single-precision 32-bit floating point number. This is like a double but holds far fewer values, such as 20.3654.

String:

A grouping of characters that most people just call text.

Remarks last but not least…

Thus we have the introduction to variables and data types in PowerShell. We discuss Objects through variables and More advanced data types in another tutorial. Join us next time for more Windows PowerShell tutorials