Loading...
X

How to assign cmdlet output to a variable and How to use a variable with saved output

Table of contents

  1. How to save cmdlet output to a variable
  2. How to display the contents of a variable in Terminal
  3. How to process the contents of a variable using a cmdlet
  4. How to count the number of rows in a variable
  5. How to get an idea of ​​the contents of a variable without printing the entire results
  6. How to display only one variable property
  7. How to check if a variable contains the desired value
  8. How to count how many times an object contains elements with a certain property
  9. How to check the type of a variable
  10. How to clear a variable value
  11. Conclusion

Let's look at PowerShell syntax related to variables. Using the syntax shown in this article, you can save the output of a cmdlet to a variable, which will allow you to use the resulting data multiple times without having to call the command over and over again. You will also learn ways to access and process the properties of an object stored in a variable without having to use the ForEach-Object and Where-Object cmdlets.

How to save cmdlet output to a variable

If you are already accustomed to the syntax of passing data from cmdlet to cmdlet through the pipeline (through the pipe, “|”), then in the case of variables, this will not work.

To save data to a variable, write it before the cmdlet and put an equal sign, for example:

$a = Get-ChildItem

If you want to assign the result of a sequence of cmdlets to a variable, then simply specify the desired command after the equal sign:

$a = Get-ChildItem | Get-Member

As you can see, you don't have to enclose the group of commands in parentheses, but you can if you want to, to make the assignment of the output to a variable more descriptive:

$a = (Get-ChildItem | Get-Member)

How to display the contents of a variable in Terminal

To display the contents of a variable, you can simply write the variable itself. If you want, you can use the echo command or the Write-Output cmdlet. All three of the following commands will produce identical results:

$a
echo $a
Write-Output $a

How to process the contents of a variable using a cmdlet

Use the following construct:

VARIABLE | CMDLET

For example:

$a | Format-Table -Property Name

How to count the number of rows in a variable

To find out the number of rows in a variable (to be more precise, to find out the number of objects in a collection), use the .Count property:

$a = Get-ChildItem
$a.Count

Strictly speaking, we do not count the number of rows, but the number of objects in the array (in the collection). Usually these values ​​are the same. But we can assume a situation when there are other arrays (collections) in the array. In this case, the number of elements will be counted only for the parent array. Consider the following example:

$b=7,8,9
$c = 1,2,3,$b

Now if we print the contents of the $c variable, we get 6 items:

$c
1
2
3
7
8
9

But the .Count property shows that there are actually only four items in the $c variable:

$c.Count
4

How to get an idea of ​​the contents of a variable without printing the entire results

With the Get-Item cmdlet, you can “look” at the contents of a variable without printing its entire contents.

The syntax is as follows (replace VARIABLE with the variable name without the “$” symbol):

Get-Item Variable:\VARIABLE

For example:

$a = Get-ChildItem
Get-Item Variable:\a

Let's check the contents of variable $c this way

$b=7,8,9
$c = 1,2,3,$b
Get-Item Variable:\c

How to display only one variable property

Instead of using additional cmdlets to filter out certain properties, you can access the variable properties directly. That is, instead of the following commands (both will give similar, though not identical, results):

$a | Format-Table -Property FullName
$a | ForEach-Object {$_.FullName}

you can use the following more compact syntax, which will give exactly the same result as the previous commands:

$a.FullName

You can print any of the properties of each variable object, for example:

$a.Name
$a.LastWriteTime

How to check if a variable contains the desired value

Suppose the variable $a contains a list of subfolders of the current folder:

$a = Get-ChildItem

And we want to know if there is a folder named “Contacts” among them? Is it possible to find out without completely enumerating all the elements of the collection?

You can check if a certain value is contained in an object using the .Contains() method. Note that this method is specified not for the entire variable, but for the property in which we are looking for the specified folder – in this case, we are looking for the “Contacts” folder in the folder names (.Name):

$a.Name.Contains('Contacts')

Note that in this case, a logical value of True or False will be output.

Also, to check whether a certain object is in the variable (more precisely, whether the value of a property of one of the objects corresponds to the specified string or number), you can do the following:

$a.Name -eq "Contacts"

In this case, instead of a logical value, a string with the searched content will be returned, or nothing will be returned if the object is not found.

How to count how many times an object contains elements with a certain property

Let's look at the following example, which counts how many processes have the “Normal” priority:

$d = Get-Process
($d | Where-Object {$_.PriorityClass -eq "Normal"}).Count

An analogue of the command discussed above is:

($d.PriorityClass -eq "Normal").Count

That is, to count objects with a certain property, you can do without the Where-Object cmdlet and use a construction like this:

(VARIABLE.PROPERTY -eq "STRING").Count

In this command:

  • VARIABLE is a variable (collection) whose objects are searched for
  • PROPERTY is a property of the collection instances by which the search is performed
  • STRING is the search string

How to check the type of a variable

First of all, it is necessary to distinguish between the type of the variable itself and the type of data stored in it.

To check the type of a variable (collection), you can use the .GetType() method:

$a.GetType()

For more details, see the article: How to determine an object’s type in PowerShell

How to clear a variable value

It would seem a trivial task – what could be simpler than clearing the value of a variable. For example, by analogy with most programming languages, you can suggest assigning an empty value:

$a = Get-ChildItem
$a = ''

But this is not exactly clearing. Now the variable contains an empty string. The output of such a variable and a truly empty (or non-existent) variable is different.

To clear variables containing an object, you can use the .Clear() method:

$a = Get-ChildItem
$a.Clear()

But when trying to apply this method to variables containing a string, an error will occur:

$a = ''
$a.Clear()

Strings do not support the .Clear() method:

InvalidOperation: Method invocation failed because [System.String] does not contain a method named 'Clear'.

A universal way to clear a variable is the Clear-Variable cmdlet:

Clear-Variable a

Please note that when using the Clear-Variable cmdlet, you do not need to put the “$” (dollar sign) before the variable name.

This method will completely clear the variable, regardless of its type.

Conclusion

This note describes the methods and properties of variables and the syntax for using them.

As you can see, sometimes you can access a variable's property directly and even search or filter for specific variable property values ​​without resorting to additional cmdlets.


Leave Your Observation

Your email address will not be published. Required fields are marked *