Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get-Content | Measure-Object -Line to Variable

Tags:

powershell

Let me preface this by saying I'm completely new to Powershell and I'm not too familiar with the language.

I'm looking to create a simple script that will count the lines of a text file and then return the result as an integer. This is the code I have so far:

$lines = Get-Content -Path "C:\File"  | Measure-Object -Line

and it returns the following:

Lines Words Characters Property
----- ----- ---------- --------
8

However, I'm looking to return the 8 as a single integer so it can be stored as a variable. Is there an easy way to do this? I appreciate any help.

Thank you!

like image 754
awilliams8976 Avatar asked Apr 14 '26 18:04

awilliams8976


1 Answers

You can use the . operator like this:

(Get-Content -Path "C:\File" | Measure-Object -Line).Lines

What you had earlier was basically an object with 4 properties: "Lines", "Words", "Characters", and "Property". Using . is an easy way to access such properties.

like image 142
Nathan W Avatar answered Apr 16 '26 12:04

Nathan W



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!