get-content | start at line # 100

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 12 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
epoh97
Posts: 102
Last visit: Wed Apr 18, 2018 6:16 am

get-content | start at line # 100

Post by epoh97 »

How can I get-content of text file and display only one or two lines from that text file starting at a specified line number?
User avatar
epoh97
Posts: 102
Last visit: Wed Apr 18, 2018 6:16 am

get-content | start at line # 100

Post by epoh97 »

How can I get-content of text file and display only one or two lines from that text file starting at a specified line number?
User avatar
EBGreen
Posts: 18
Last visit: Wed Mar 20, 2013 8:45 am

get-content | start at line # 100

Post by EBGreen »

Well one way is:

$fileContent = Get-Content 'C:PathToFile.txt'
$fileContent[99..100]


That would get you line 100 and 101.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

get-content | start at line # 100

Post by jvierra »

FYI - some shorthand.

Code: Select all

	
#return lines 4 through 8
	
(Get-Content 'C:PathToFile.txt')[3..7] 
#return line 3, 8 and 10
	

	
(Get-Content 'C:PathToFile.txt')[2,7,9] 
#return last line
	

	
(Get-Content 'C:PathToFile.txt')[-1] 
	
# reverse file lines
	

	
$s=Get-Content 'C:PathToFile.txt'
	
$s[$s.length-1..0]
	

See: help array
This topic is 12 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked