2

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?

mklement0
  • 382,024
  • 64
  • 607
  • 775

2 Answers2

2

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?

js2010
  • 23,033
  • 6
  • 64
  • 66
0

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]
oɔɯǝɹ
  • 7,219
  • 7
  • 58
  • 69