user11880817
user11880817

Reputation:

How do I return only the integer from Measure-Object in PowerShell?

I would like to run a piece of code that counts how many characters are in a text file and save it as another text file but I need the output to only be a number.

This is the code I run in PowerShell:

Get-Content [File location] | Measure-Object -Character | Out-File -FilePath [Output Location]

and it saves the output like this:

Lines Words Characters Property
----- ----- ---------- --------
                     1         

Is there any way to save just the number?

Upvotes: 2

Views: 3038

Answers (2)

oɔɯǝɹ
oɔɯǝɹ

Reputation: 7625

Anything is an object in PowerShell, that goes for the result of Measure-Object as well. To get just the value of a property, use Select-Object -ExpandProperty <PropertyName>to get the desired properties Value;

PS> Get-ChildItem | Measure-Object | Select-Object -ExpandProperty Count
PS> 3

In your example:

PS> Get-Content [File location] | 
    Measure-Object | 
    Select-Object -ExpandProperty Count | 
    Out-File -FilePath [Output Location]

Upvotes: -1

js2010
js2010

Reputation: 27473

Basic powershell:

(Get-Content file | Measure-Object -Character).characters

or

Get-Content file | Measure-Object -Character | select -expand characters

Related: How to get an object's property's value by property name?

Upvotes: 2

Related Questions