none
Picking a value from output in Powershell? RRS feed

  • Question

  • How can I capture the value to a variable in the following output?

    When I execute the command Get-VM I am getting the following:

                                                                                                                                                                                               

    Host                      VMElementName             State        Up-Time (mS) Owner                                                                                                                                                                                
    --------                  -------------                     -----        ------------ -----                                                                                                                                                                                
    INDIA                     Win10                          Stopped      0     

    How can I get the value Stopped from the above out put? -Thanks in advance

    • Moved by Bill_Stewart Thursday, January 25, 2018 10:15 PM Unanswerable drive-by question
    Friday, November 10, 2017 12:21 AM

All replies

  • $state = Get-Vm |select -expand State

    \_(ツ)_/

    Friday, November 10, 2017 12:27 AM
  • Thanks JRV. I got the following error

    Select-Object : Property "State" cannot be found.
    At line:1 char:15
    + Get-Vm |select <<<<  -expand State
        + CategoryInfo          : InvalidArgument: (\\INDIA\root\vi...2-6056703708ED":PSObject) [Select-Object], PSArgumentException
        + FullyQualifiedErrorId : ExpandPropertyNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand

    Friday, November 10, 2017 12:36 AM
  • Works fine for the rest of us.

    PS D:\scripts> $state = Get-Vm | select -expand state
    PS D:\scripts> $state
    Off
    Off
    Off
    Off
    PS D:\scripts>


    \_(ツ)_/

    Friday, November 10, 2017 12:40 AM
  • I am using Windows Server 2008R2 for hosting. Will it make any difference?
    Friday, November 10, 2017 1:06 AM
  • Hosting what?  You have to be more accurate with your questions.

    \_(ツ)_/

    Friday, November 10, 2017 1:16 AM
  • I am very sorry. I am using Hyper-V on Windows Server 2008R2 and built Win10 Virtual Machine.
    Friday, November 10, 2017 1:18 AM
  • I see no issues.

    Are you sure your example is correct?


    \_(ツ)_/

    Friday, November 10, 2017 1:22 AM
  • 
    Friday, November 10, 2017 1:31 AM
  • Sounds like you have a broken system.


    \_(ツ)_/

    Friday, November 10, 2017 1:37 AM
  • You mean that my powershell component corrupted? Is there any way to fix it?
    Friday, November 10, 2017 1:40 AM
  • You will have contact MS Support to help fix your system or contact a consultant.  Your issue is not a scripting issue.


    \_(ツ)_/

    Friday, November 10, 2017 1:48 AM