header1   header
header
header : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
IMPORTANT: PowerShellCommunity.org is moving! - Wednesday, August 15, 2012

PowerShellCommunity.org is moving!  This community software, and the hardware that it sits on, are no longer serving the purposes of this community.  As a result, we have decided to move this community to a new home at PowerShell.org.  PowerShell.org is already up and running with the new community software and in its new location, so please post any new questions that you have on the forums over there instead of posting them on this site.  We've already started getting some great questions from members of the community over there so please, come on over and join us!

While we are going through this transition, this site will remain up for the short term.  New posts may no longer be created on these forums, however replies to existing posts are allowed so that users who posted questions don't have to re-post the same question on the new site.

[UPDATE 28/02/2013] New user registration has been disabled and forums have now been switched to read-only, including for existing posts since all threads that were started should now be completed. If you have a question about content on this site or about PowerShell in general, head over to PowerShell.org and ask it there where there are people actively using the site and answering questions.

If you have any questions, please let us know on the PowerShell.org site.

Thank you,

Kirk "Poshoholic" Munro

 
How do I put Count Line numbers on an output
Last Post 30 Jun 2008 05:50 AM by Brandon Shell [MVP]. 4 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
edm365f31User is Offline
Basic Member
Basic Member
Posts:100
Avatar

--
26 Jun 2008 08:57 AM

    I have a shoprocline.ps1 script that query for same process and a bunch of them. I wanted to know how many process is by having counting the line output in powershell command shell how do I do this?

    halr9000User is Offline
    PowerShell MVP, Site Admin
    Advanced Member
    Advanced Member
    Posts:565
    Avatar

    --
    26 Jun 2008 09:37 AM
    There's a few options:

    1. Most objects have a Length property, you can access that.
    2. For loops use a counter which you can access, e.g.:

    for ($i = 1; $i -le (gps).length; $i++) { $i }

    3. Pipe anything to Measure-Object, e.g.:

    gps | measure-object
    Community Director, PowerShellCommunity.org
    Co-host, PowerScripting Podcast
    Author, TechProsaic
    glnsizeUser is Offline
    Basic Member
    Basic Member
    Posts:193

    --
    28 Jun 2008 07:02 AM

     

    Without forcing it powershell never outputs text.  Therefor your not doing a line count.  Instead you just ask pipeline a question. like..

    Show me all process with more then one running instance?

    gwmi win32_process | group-object name | ? { $_.count -gt 1}

    Instead of me worrying about how to parse the data.  I used the group-Object cmdlet to group process by name.  The output of that cmdlet contains a count property.

    If you knew exactly what process you where tageting you could also do this.

    (gwmi win32_process -filter 'name="svchost.exe"').count

    In that case the gwmi command is executed first with in the ().  Since the -filter removed all but the process in question, you can use .count to count how many object were returned.  Hope that helped...

    -Glenn

     

    halr9000User is Offline
    PowerShell MVP, Site Admin
    Advanced Member
    Advanced Member
    Posts:565
    Avatar

    --
    28 Jun 2008 08:17 AM
    Notes:
    gps is alias for Get-Process
    gwmi is alias for Get-WMIObject

    If you are only working with local processes, then Get-Process is a little simpler to work with. "gwmi win32_process" can work with remote computers.
    Community Director, PowerShellCommunity.org
    Co-host, PowerScripting Podcast
    Author, TechProsaic
    Brandon Shell [MVP]User is Offline
    Basic Member
    Basic Member
    Posts:396
    Avatar

    --
    30 Jun 2008 05:50 AM
    (gwmi win32_process).count
    Brandon Shell
    ----------------
    Microsoft Powershell MVP
    https://mvp.support.microsoft.com/profile/Brandon
    Blog: http://www.bsonposh.com
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Many thanks to our original sponsors: Quest Software • SAPIEN Technologies • Compellent • Microsoft footer
    footer   footer