top of page
Writer's pictureUzair Ansari

Powershell variables

Updated: Nov 5, 2021


Have you ever wondered what is powershell variable? Or did you ever asked yourself what is a variable in a scripting language? Here is the right place to understand a variable.

In simple terms, variable can be defined as temporary storage for the data in a scripting environment. This means any data which is generated through some command can be stored in a variable for further processing.

Lets understand this with an example. Here we use Get-Item command to fetch the details of the file "New File.txt". The command does so by displaying the file details on the powershell console. This is because powershell automatically adds Out-Default to the end of every pipeline. Out-Default decides how to format and output the object stream. If the object stream is a stream of strings, Out-Default pipes these directly to Out-Host







In this case the output is displayed on the powershell console and we will not able to process the output.

We will now see what options do we have if we store the output of the command in a variable.






As we can see in the first command. The result of the Get-Item command is stored in a powershell variable named Data. Powershell uses $ to denote the variable. The $ symbol will be preceded with the variable name.

This will store all the properties and methods of the 'New File.txt' in variable named Data. We can than use these properties and methods and process them according to our requirement.

In the second command in the above image we used FullName property to display the full name of the file.

Similarly in the third command we used the FullName property to be exported in a text file named FileDetails.txt instead of displaying on the powershell console.

Full list of properties and methods can be viewed using Get-Member command.



Until now we saw how we can view and use the properties of the variable. Now we will see how we can change or set the property. We will first view the LastAccessTime property of the file.







Now we will set or modify the LastAccessTime property.







In the above example we used the Get-Date command to retrieve the current date and set it as the LastAccessTime property of the file. This changed the LastAccessTime property from '29 April 2020 00:06:35' to '29 April 2020 00:22:31'.


The examples given above depicts how we can use variables to process object properties. However storing the data in a variable and then altering it is just one of the ways. There are other ways where we can read and write the properties of an object without storing the object data in a variable. It is not mandatory to store powershell objects in variable to process them. We just used it as an example to understand the concept of a variable.



Assigning value to multiple powershell variables


You can assign a value to multiple powershell variable at once using the method given below:











Assigning values to multiple variables at the same time


You can assign different values to different variables by using the method given below:









Search variables


Variable: virtual drive can be used to search variables by using wildcards. If you'd like to see all the variables whose name starts with "$var", try this:










Comments


bottom of page