ifthenifthen
ifthenifthen

Reputation: 275

'Measure-Object' not working in windows powershell - how to do basic sums?

I have windows 7 at work. I don't know what version of powershell that I have, but it says it is from 2009. I have been using Google-Fu to find out how to do some basic math in powershell - sums, specific sums, etc, but what I am finding isn't working - I think it is because I am using a very old version of powershell.

For instance, this command works:

gc filename -totalcount 1000

This command does not work:

gc filename -head 1000

When I run:

gc filename Measure-Object "Column Name" -Sum

I get the error:

"Get-Content: A positional parameter cannot be found that accepts argument 'Measure-Object'.

Can anyone help? Or point me to a resource that has older commands?

Upvotes: 1

Views: 1766

Answers (2)

Tom Collins
Tom Collins

Reputation: 86

Find your PowerShell version using this code snippet. As noted above, 2.0 is the default for Windows 7.

    $PSVersionTable.PSVersion

Secondly, I highly recommend (unless you have specific version constraints) to upgrade to PowerShell 5.1 as there were some bugs in the older versions that have been fixed. https://www.microsoft.com/en-us/download/details.aspx?id=54616

Third you likely need to do more manipulation of the file after using Get-Content before you can measure it. If it's a CSV you may need to specify the specific column you want to manipulate. If you provide more detail of what you are trying to do in a general sense, it'd be easier to provide more targeted feedback.

Upvotes: 0

henrycarteruk
henrycarteruk

Reputation: 13227

You need to use the pipeline to pass one object to another command:

Get-Content filename | Measure-Object

I'd also recommend reading the documentation on Measure-Object so you know how to use it correctly.

Upvotes: 2

Related Questions