Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Need help with cmdlet output

Need help with cmdlet output

Need help with cmdlet output

Hi all,

Been fiddling with PowerShell last couple of days, but there's this thing I can't get my head around.

Let's say I'd like some message if HTTP-GET requests are happening on SomeSite. I'd think I'd need a syntax like:

if((Get-Counter -counter "\Web Service(SomeSite)\Get Requests/sec") -ne 0) {Write-Host "We received" $_ "GET requests last sec"}

The above does not work, because Get-Counter, like all cmdlets, outputs (a table showing) objects of various types, inlcuding a header. In this example an object of type 'System.Object.PerformanceCounterSampleSet' is returned.

I'm used to cmd scripting, and never had to deal with anything else then strings and ints. I'm confused how I can get Get-Counter to return just an int value.

RE: Need help with cmdlet output

Hi jmeckley,

I can't get your code to work. Using just the...


Get-Counter -counter "\Web Service(SomeSite)\Get Requests/sec"
...part from my example, I was at least able to get data from the right counter. Considering your code, I have the following questions...


get-counter | where {$_.Name == "\Web Service(SomeSite)\Get Requests/sec"} ..etc..
Are you sure the Get-Counter cmdlet is returning objects that have a .Name property? How to determine this? Also, the '==' condition comparisson is invalid in powershell, you're probably used to C# or Java, right? In powershell, use '-eq'.


select {$_.Count}
Don't understand what you're trying to do here. Assuming you agree on $_ to represent an array (with possible various object types), I can't see why you'd call the .Count property, which will return the number of objects in the array.


RE: Need help with cmdlet output

i'm sure there are bugs in the script as I wrote this from memory without testing it.

if the property isn't Name it's something else.
when piping from one command to the next ps will iterate over the collection allowing you to process each object individually. $_ references the current item in the collection. so $_.Count would give you the value of the Count property on the current object, assuming the Count property exists.

powershell pipes objects from one cmdlet to the next, not strings or integers. if you just want a count of the webserver counts it might look like this


$counters = Get-Counter -counter "\Web Service(SomeSite)\Get Requests/sec"
write-host $counters.length

Jason Meckley
Senior Programmer

FAQ855-7190: Database Connection Management
FAQ732-7259: Keeping the UI responsive
My Blog

RE: Need help with cmdlet output

Hi jmeckley,

Thanks for the info above. Based on your comment I started googling again, and stumbled upon the online PowerShell SDK library, and the Get-Method Cmdlet, which will display all properties and methods available for any given cmdlet.

The code I used in this thread was just an example; what I really wanted was to understand how to parse cmdlet output. Knowing how to show available methods and properties for cmdlets is crucial for doing this.

Working line:


$gets = [float]((Get-Counter -counter "\Web Service(SomeSite)\Get Requests/sec").CounterSamples | select CookedValue).CookedValue.ToString()

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close