The Start and End of a File: -TotalCount, -Tail and -Wait
Get-Content -TotalCount shows only a file's first lines and -Tail only its last, and -Wait keeps a file open on screen so every line added to it appears as it's written, until Ctrl+C stops it.
Apprentice · PowerShell · Operating System · Updated
After this lesson you can
- Show the first or last lines of a file with Get-Content -TotalCount and -Tail
- Watch a file grow with Get-Content -Wait, and stop watching with Ctrl+C
Key takeaways
Get-Contentwith-TotalCount 3shows a file's first 3 lines;-Headand-Firstmean the same.-Tail 3shows the last 3 lines. In a log file, those are the newest.-Waitkeeps watching a file and prints each new line as it's added. Ctrl+C stops watching.
A packing list can run to pages, but the title and the first few boxes tell you what you need. A program's log can hold ten thousand lines, and the newest ones, at the bottom, are the ones that say why it crashed a minute ago. And sometimes you want to watch: a download, a game server, a website starting up on your computer, writing a line each time something happens. Get-Content on its own prints every line, top to bottom. Three of its parameters print only the part you need.
This lesson works in the moving-day folder from Deleting Safely: Remove-Item, -WhatIf and -Confirm (any folder works, if you'd rather). Make the packing list with this one command; a line may break after a comma, so it's written one box to a line:
Set-Location ~\terminal-practice\moving-day
Set-Content packing-list.txt 'PACKING LIST: 14 BOXES',
'box 1: books (heavy, lift with your knees)',
'box 2: kitchen: plates, mugs, kettle',
'box 3: bedroom: pillow, lamp, socks',
'box 4: bathroom: towels, shower curtain',
'box 5: living room: cushions, rug',
'box 6: desk: laptop charger, notebooks',
'box 7: board games and puzzles',
'box 8: winter coats',
'box 9: shoes',
'box 10: plants (this side up)',
'box 11: pictures for the walls',
'box 12: cleaning supplies',
'box 13: tools: hammer, tape, screwdriver',
'box 14: bubble wrap. So much bubble wrap.',
'NOTE: the TV remote is taped to the bottom of box 7'
The first lines: -TotalCount
-TotalCount (read "total count") stops after that many lines:
Get-Content packing-list.txt -TotalCount 3
PACKING LIST: 14 BOXES
box 1: books (heavy, lift with your knees)
box 2: kitchen: plates, mugs, kettle
The title and the first two boxes, and nothing scrolls past. -Head and -First are other names for the same parameter, so Get-Content packing-list.txt -Head 3 does the same. -TotalCount 1 is a quick way to read a file's title line, or the header row of a spreadsheet saved as a .csv file.
The last lines: -Tail
-Tail shows the end of a file instead. Packing lists aren't the only files with something important at the bottom:
Get-Content packing-list.txt -Tail 1
NOTE: the TV remote is taped to the bottom of box 7
The end matters most in a log file, a file a program writes a line to each time something happens, the newest line at the bottom. The movers keep one. Make it, then read its last two lines:
Set-Content movers.log '09:02 truck parked outside', '09:10 box 1 carried up: books', '09:18 box 2 carried up: kitchen', '09:25 box 3 carried up: bedroom', '09:31 lunch break called off: too much to do'
Get-Content movers.log -Tail 2
09:25 box 3 carried up: bedroom
09:31 lunch break called off: too much to do
-Last is another name for -Tail. On a real computer, an app that misbehaves often leaves a log, and -Tail 20 on it is where an IT help-desk tech or a developer starts looking.
Watching a file grow: -Wait
-Wait doesn't stop at the end of the file. It prints the lines you asked for, then keeps watching, and each line added to the file appears on your screen as it's written. Start watching the movers' log:
Get-Content movers.log -Tail 2 -Wait
The prompt doesn't come back: PowerShell is watching. Open a second tab in Windows Terminal (Ctrl+Shift+T), and play the movers there, one line at a time:
Set-Location ~\terminal-practice\moving-day
Add-Content movers.log '09:40 box 4 carried up: bathroom'
Add-Content movers.log '09:47 box 5 carried up: living room'
Each new line shows up in the first tab within a second or so:
09:25 box 3 carried up: bedroom
09:31 lunch break called off: too much to do
09:40 box 4 carried up: bathroom
09:47 box 5 carried up: living room
When you've seen enough, go back to the first tab and press Ctrl+C, with no text selected. It stops the watching and gives you the prompt back. This is how people keep an eye on a website or a game server they're running: one window does the work, and another watches its log.
On a Mac or Linux, all three work the same, with / in paths. Windows PowerShell 5.1 has them too.
Try it in the terminal
Terminal Quest: Moving Day, goal 1: played right on the lesson page.
Moving Day starts with a long packing list in the hallway. Read only the top of it, the part that matters.
Check yourself
2 questions to try on the lesson page.
So what
The useful part of a big file is usually at one end: the title at the top, the newest news at the bottom. -TotalCount, -Tail and -Wait take you straight there, and save you scrolling through everything in between.