powershellmeasure-object

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?


Solution

  • 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?