Skip to main content
question2
Source Link
Lgn
  • 10.2k
  • 5
  • 22
  • 26

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

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.

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
Source Link
Lgn
  • 10.2k
  • 5
  • 22
  • 26

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

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.