PowerShell Variable Tutorial

PowerShell is a very advanced scripting and programming language which supports the variables. Variables in PowerShell can be used to store data like integer, number, string, text, date, time, object, etc. For example, a PowerShell variable can be store a command output in a structured way as we can also call it an object.

Define Variable

In PowerShell, variables can be defined in different ways. A variable is prefixed with the $ sign in order to express that this a variable. In the following example, we will create a variable named $myvar . We will do not set any data into this variable which means a variable defined without any first value will be an empty variable.

$myvar

echo $myvar
# Output will be empty 

Alternatively we can create or define a variable by assigning as value to it. In the following example we will create a new variable $age and set 25 as value.

$age = 25

PowerShell provides commands or cmdlets. These commands results can be also used to create a variable and set the value of the variable. In the following example, we will use the Get-Process command which returns currently running processes. The returned value will be set for the variable named $process.

$process = Get-Process

Variable Types

PowerShell is object-oriented programming and scripting language. PowerShell provides advanced features like a complete programming language. The PowerShell commands and types are derived from the .NET framework which is the infrastructure of the C#, Vb.NET programming languages. So PowerShell provides different variable types in order to store the variable data. These variable types are decided automatically by the PowerShell according to the data type.

# Integer Type
$age = 25

# String Type
$name = "İsmail Baydan"

# Array Type
$mylist =  1 , 2 , 3

# DateTime Type
$today = (Get-Date).DateTime

As an object-oriented and .NET supported programming language it supports different object types. So the PowerShell provides complex types for different classes.

READ  PowerShell Get-Date Command Tutorial

String Variable Operations

The string is a popular variable type and used for different cases. Also, the string variables can be used in different operations. In the following example, we will provide some samples about the string variable operations.

$hello = "Hello"

$windowstect = "WindowsTect"

#Join, Add or Concatenate Two String Variables and A String Value
$str = $hello+" "+$windowstect


#Convert integer 25 into the string variable
$age = [string]35

PowerShell provides the New-Variable command which is used to create and define new variable with different properties like Name, Vsibility/Scope, Value etc.

New-Variable -Name "age" -Visibility Public -Value 35

Print Variable Type

In PowerShell every variable is an object even it contains basic values like string, integer etc. PowerShell objects provide the GetType() method which returns the type of the given object.

$age = 35

$age.GetType()
Print Variable Type

As the GetType() method returns the type object it provides the full name of the given object type with the FullName attribute like below.

$age = 35

$age.GetType().FullName
#System.In32

List All Variables

All PowerShell variables can be listed by using the ls variable:* or Get-Variable commands. These commands will list currently existing variables with their values.

ls variable:*

Or

Get-Variable
List All Variables

Change Variable Value

After create a variable wecan change its value. As the term suggest the variable its content and data is also variable. A variable value can be changed by using the equal sign which is used assignment.

$age = 25

PowerShell also provides the Set-Variable command in order to change and set variables. The set variable is very similar to the new variable where the -Value property should provide the new value like below.

Set-Variable -Name $age -Value 25

Clear Variable or Set Variable AsNull

Variables are created and used to store values. But after using a variable we may clear the variable where this variable value or content will be set as Null. There are different ways to clear variables or set null. The Clear-Variable can be used to clear the variable value completely or the $null can be assigned into a variable to clear it. We will also use the -Name property for the Clear-Variable command in order to provide the variable name to clear.

Clear-Variable -Name $age

$age = $null

Print or Export Variables

Variables value can be printed and exported by using the echo, Get-Content, Out-File, Export-CSV commands. By using the echo command a variable content can be printed in PowerShell. The echo command is natively a Linux command and provided by the PowerShell in order to make users Linux users comfortable with its shell environment.

echo $age

The official command to print a variable in PowerShell is the Get-Content.

Get-Content $age

Out-File is used to print the given object or variable content into the specified file.

Out-File $age

CSV is a file format where columns are separated with comma by default. A variable can be printed with the Export-CSV method like below.

Export-CSV $age

Remove Variable

After a variable is created in PowerShell is exist until the current session ends or script ends. But this may create collusion if we need to create new variables. But existing variables can be removed by using the Remote-Variable command or Remove-Item command.

READ  How to Create and Run Batch (BAT) File On Windows?

In order to remove or delete a variable with the Remove-Variable command the -Name property should be used to specify the variable name. In the following example, we will delete the variable named $age.

$age = 35

Remove-Variable $age

The Remove-Item command is used to remove different data from the PowerShell. It is a generic command to remove registry, objects, etc. So in order to remove a variable the -Path property will be used with the Variable:\VARNAME type with the VARNAME which is the variable name. In the following example, we will remove the variable named $age.

$age = 35

Remove-Item -Path Variable:\age

Leave a Comment