none
In PowerShell I need to get all of my running services. Will this work? RRS feed

  • Question

  • Get-Service | where status -eq "running"

    It seems to be very explicit but how does it work?


    jv
    Tuesday, October 4, 2011 10:15 PM

Answers

  • That will only work in PowerShell v3.0.  In v2.0 you will still need to include the '?{$_.Status -eq "Running"}.  PoSh v3 does include the v2 method for backwards compatibility.

    My guess is that it uses Where-Object to do a [Parameter(ValueFromPipelineByPropertyName=$True)] on the 'status' property and matches it to the value "Running".


    If you found this post helpful, please "Vote as Helpful". If it answered your question, remember to "Mark as Answer".

    Rich Prescott | MCITP, MCTS, MCP

    [Blog] Engineering Efficiency | [Twitter] @Rich_Prescott | [Powershell GUI] Client System Administration tool | [PowerShell GUI] New AD User Creation tool
    Tuesday, October 4, 2011 11:24 PM
    Moderator

All replies

  • That will only work in PowerShell v3.0.  In v2.0 you will still need to include the '?{$_.Status -eq "Running"}.  PoSh v3 does include the v2 method for backwards compatibility.

    My guess is that it uses Where-Object to do a [Parameter(ValueFromPipelineByPropertyName=$True)] on the 'status' property and matches it to the value "Running".


    If you found this post helpful, please "Vote as Helpful". If it answered your question, remember to "Mark as Answer".

    Rich Prescott | MCITP, MCTS, MCP

    [Blog] Engineering Efficiency | [Twitter] @Rich_Prescott | [Powershell GUI] Client System Administration tool | [PowerShell GUI] New AD User Creation tool
    Tuesday, October 4, 2011 11:24 PM
    Moderator
  • Aha! Another Posh 3.0 user.

    I wonder if anyone else knows this....

    Anything to save a keysroke and to make use more intuitive. Wit Selet-Object and othr CmdLets we currently (2.0) don't have to use the ugly '$_' pipe object.  Now we may never have to use it,

    http://mcpmag.com/articles/2011/10/03/powershell-vnext-vbest.aspx


    jv
    Wednesday, October 5, 2011 12:34 AM
  • That information and more is included in the release notes that comes with the Windows Management Framework 3.0 CTP1 download.  I suggest you take a look at workflows.  From what I have seen of your scripting on these forums, you will like it.
    If you found this post helpful, please "Vote as Helpful". If it answered your question, remember to "Mark as Answer".

    Rich Prescott | MCITP, MCTS, MCP | Infrastructure Architect, Windows Engineer and PowerShell blogger

    [Blog] Engineering Efficiency | [Twitter] @Rich_Prescott | [Powershell GUI] Client System Administration tool | [PowerShell GUI] New AD User Creation tool
    Wednesday, October 5, 2011 12:46 AM
    Moderator
  • Get-Service | where status -eq "running"

    It seems to be very explicit but how does it work?


    jv


    What is "where" in this case?  Is it an alias for Where-Object? 

    How did you find out about this new syntax?


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    Wednesday, October 5, 2011 5:33 AM
  • Get-Service | where status -eq "running"

    It seems to be very explicit but how does it work?


    jv


    What is "where" in this case?  Is it an alias for Where-Object? 

    How did you find out about this new syntax?


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"

    Have you tried it? 

    I have an even cleaner version:

    Get-Service | where status -eq running

    Too many of us insist on putting quotes on everything,  They are unnecessary most of the time.

    PLus I can shortewn it even more:

    Get-Service | where status -eq r

    Som of tis magic is accomplished because -eq is an argument and not an operator and the first positional parameter is -Property.  (help where -full). Like Select-Object  can be shortened to select prop1,prop2,prop3..., where-object can be shortened the same way.

    Get-Service | where {$_.status -eq 'running'}
    Get-Service | where status -eq r

    Both syntaxes work because of parametersets.  Compare help where between WMF 2.0 and WMF 3.0.

    http://mcpmag.com/articles/2011/10/03/powershell-vnext-vbest.aspx




    jv
    Wednesday, October 5, 2011 8:55 AM