Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store a cmdlet's result value in a variable in Powershell

Tags:

I would like to run a cmdlet and store the result's value in a variable.

For example

C:\PS>Get-WSManInstance -enumerate wmicimv2/win32_process | select Priority 

It lists priorities with a header. The first one for example:

Priority -------- 8 

How can i store them in a variable? I've tried:

$var=Get-WSManInstance -enumerate wmicimv2/win32_process | select Priority 

Now the variable is: @{Priority=8} and I wanted it to be 8.

Question 2:

Can I store two variables with one cmdlet? I mean store it after the pipeline.

C:\PS>Get-WSManInstance -enumerate wmicimv2/win32_process | select Priority, ProcessID 

I would like to avoid this:

$prio=Get-WSManInstance -enumerate wmicimv2/win32_process | select Priority $pid=Get-WSManInstance -enumerate wmicimv2/win32_process | select ProcessID 
like image 769
Lgn Avatar asked Apr 09 '12 12:04

Lgn


People also ask

How do you store values in a variable in PowerShell?

To create a new variable, use an assignment statement to assign a value to the variable. You don't have to declare the variable before using it. The default value of all variables is $null . To get a list of all the variables in your PowerShell session, type Get-Variable .

How do I store multiple values in a variable in PowerShell?

In PowerShell, you can assign values to multiple variables using a single command. The first element of the assignment value is assigned to the first variable, the second element is assigned to the second variable, the third element to the third variable. This is known as multiple assignment.

What is the $_ variable in PowerShell?

The “$_” is said to be the pipeline variable in PowerShell. The “$_” variable is an alias to PowerShell's automatic variable named “$PSItem“. It has multiple use cases such as filtering an item or referring to any specific object.


2 Answers

Use the -ExpandProperty flag of Select-Object

$var=Get-WSManInstance -enumerate wmicimv2/win32_process | select -expand Priority 

Update to answer the other question:

Note that you can as well just access the property:

$var=(Get-WSManInstance -enumerate wmicimv2/win32_process).Priority 

So to get multiple of these into variables:

$var=Get-WSManInstance -enumerate wmicimv2/win32_process    $prio = $var.Priority    $pid = $var.ProcessID 
like image 58
manojlds Avatar answered Sep 30 '22 21:09

manojlds


Just access the Priority property of the object returned from the pipeline:

$var = (Get-WSManInstance -enumerate wmicimv2/win32_process).Priority 

(This won't work if Get-WSManInstance returns multiple objects.2)

For the second question: to get two properties there are several options, problably the simplest is to have have one variable* containing an object with two separate properties:

$var = (Get-WSManInstance -enumerate wmicimv2/win32_process | select -first 1 Priority, ProcessID) 

and then use, assuming only one process:

$var.Priority 

and

$var.ProcessID 

If there are multiple processes $var will be an array which you can index, so to get the properties of the first process (using the array literal syntax @(...) so it is always a collection1):

$var = @(Get-WSManInstance -enumerate wmicimv2/win32_process | select -first 1 Priority, ProcessID) 

and then use:

$var[0].Priority $var[0].ProcessID 

1 PowerShell helpfully for the command line, but not so helpfully in scripts has some extra logic when assigning the result of a pipeline to a variable: if no objects are returned then set $null, if one is returned then that object is assigned, otherwise an array is assigned. Forcing an array returns an array with zero, one or more (respectively) elements.

2 This changes in PowerShell V3 (at the time of writing in Release Candidate), using a member property on an array of objects will return an array of the value of those properties.

like image 21
Richard Avatar answered Sep 30 '22 20:09

Richard