Variables In PowerShell

Variables In PowerShell

Variables In PowerShell

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed what is PowerShell and how to get started with PowerShell.

https://cloudaffaire.com/getting-started-with-powershell/

In this blog post, we will discuss variables in PowerShell. Like any other scripting language, PowerShell also supports variables. Variable in a nutshell is a temporary space to store some value. In PowerShell, you can store different data types as a variable value. Variables can be user-defined or system-defined and the scope of the variables can be local or global.

A variable is defined as $<variable_name> = <variable_value> and variable value can be retrieved using $<variable_name>. Variable names aren’t case-sensitive, and can include spaces and special characters. But variable names that include special characters and spaces are difficult to use and should be avoided.

Variables In PowerShell:

You can define a new variable using $<variable_name> = <variable_value> syntax. You don’t need to explicitly define variable data type as PowerShell automatically determines the variable data type based on the assigned value. Variable can be reassigned a different value based on the requirement. To get all variables available in current session you can use cmdlet Get-Variable.

PowerShell Variables Data Type:

PowerShell variables are loosely typed, which means that they aren’t limited to a particular type of object. A single variable can contain integers, strings, characters, arrays, hash tables or a collection of different types of objects etc. You can get the a variable data type using GetType() in-built function.

PowerShell Variable Typecasting:

You can use a type attribute and cast notation to ensure that a variable can contain only specific object types or objects that can be converted to that type. If you try to assign a value of another type, PowerShell tries to convert the value to its type. If the type can’t be converted, the assignment statement fails. To use cast notation, enter a type name, enclosed in brackets, before the variable name (on the left side of the assignment statement).

PowerShell Variable Scope:

A variable can have different scope based on the variable declaration method used and by default, variables are only available in the scope in which they’re created. For example, a variable that you create in a function is available only within the function. A variable that you create in a script is available only within the script. If you dot-source the script, the variable is added to the current scope. You can use a scope modifier to change the default scope of the variable. Below is the different scope available in PowerShell –

  • Global: The scope that is in effect when PowerShell starts. Variables and functions that are present when PowerShell starts have been created in the global scope, such as automatic variables and preference variables. The variables, aliases, and functions in your PowerShell profiles are also created in the global scope.
  • Local: The current scope. The local scope can be the global scope or any other scope.
  • Script: The scope that is created while a script file runs. Only the commands in the script run in the script scope. To the commands in a script, the script scope is the local scope.
  • Private: Private is not a scope. It is an option that changes the visibility of an item outside of the scope where the item is defined.

PowerShell Variable Types:

A variable can be user defined or generated by the system itself. Below are different types of variables available in PowerShell

  • User-created variables: User-created variables are created and maintained by the user. By default, the variables that you create at the PowerShell command line exist only while the PowerShell window is open. When the PowerShell windows is closed, the variables are deleted. To save a variable, add it to your PowerShell profile. You can also create variables in scripts with global, script, or local scope.
  • Automatic variables: Automatic variables store the state of PowerShell. These variables are created by PowerShell, and PowerShell changes their values as required to maintain their accuracy. Users can’t change the value of these variables. For example, the $PSHOME variable stores the path to the PowerShell installation directory.
  • Preference variables: Preference variables store user preferences for PowerShell. These variables are created by PowerShell and are populated with default values. Users can change the values of these variables. For example, the $MaximumHistoryCount variable determines the maximum number of entries in the session history.

PowerShell Variable cmdlets:

PowerShell includes a set of cmdlets that are designed to manage variables. Below is the list of available cmdlets

  • Clear-Variable: Deletes the value of a variable.
  • Get-Variable: Gets the variables in the current console.
  • New-Variable: Creates a new variable.
  • Remove-Variable: Deletes a variable and its value.
  • Set-Variable: Changes the value of a variable.

Hope you have enjoyed this article. In the next blog post, we will discuss operators in PowerShell.

To get more details on PowerShell, kindly follow below official documentation

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

 

Leave a Reply

Close Menu