header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

How do I put Count Line numbers on an output
Last Post 30 Jun 2008 02:50 PM by bsonposh. 4 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
edm365f31User is Offline
New Member
New Member
Posts:39
Avatar

--
26 Jun 2008 05:57 PM  

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
Basic Member
Basic Member
Posts:335
Avatar

--
26 Jun 2008 06:37 PM  
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 Online
Basic Member
Basic Member
Posts:101

--
28 Jun 2008 04:02 PM  

 

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
Basic Member
Basic Member
Posts:335
Avatar

--
28 Jun 2008 05:17 PM  
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
bsonposhUser is Offline
Basic Member
Basic Member
Posts:393
Avatar

--
30 Jun 2008 02:50 PM  
(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.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer