Reading Files: Get-Content
Get-Content prints a text file's lines right in PowerShell, by its name or its path, more shows a long file one screen at a time, and edit opens a file you want to change.
Novice · PowerShell · Operating System · Updated
After this lesson you can
- Read a text file with Get-Content, by its name or by its path
- Choose between Get-Content, more and an editor for a short file, a long file, or a file to change
Key takeaways
Get-Contentprints a text file's lines. Give it the file's name, or its path when it's in another folder.- Text files (
.txt,.csv,.json,.log,.md) read well. A photo or a program isn't text, so it prints as gibberish. - For a long file,
Get-Contentwith| moreshows one screen at a time. Space shows the next screen, and Q stops. editopens a file in Microsoft Edit, inside the terminal, when you want to scroll, search or change it.
A project you download comes with a README.txt that says how to start it. An app writes what went wrong into a log file. A tutorial says "check what's in settings.json". You could find each file in File Explorer and open it in Notepad, and sometimes that's the best choice. But when you're already in PowerShell, or on a server that has no Notepad and no mouse, it's quicker to read the file right where you are.
This lesson uses the trip folder from Where Am I? Get-Location and Paths.
Reading a file: Get-Content
Get-Content (read "get content") prints a file's text, line by line. Go to the trip folder and read the itinerary:
Set-Location ~\terminal-practice\trip
Get-Content itinerary.txt
Day 1: the beach. Swim, build a sandcastle, watch the sunset from the pier.
Day 2: the old town. The market in the morning, the clock tower at noon.
Day 3: the mountains. Hike to the summit, stop at the waterfall on the way.
Tab works for file names too: type Get-Content it and press Tab, and PowerShell fills in .\itinerary.txt. If you get a name wrong, the red message names the path it looked for: Cannot find path 'C:\Users\Sam\terminal-practice\trip\itinerery.txt' because it does not exist. Compare it with the real name, letter by letter.
The short names for Get-Content are cat, type and gc (on a Mac or Linux, cat runs the system's own cat program instead).
A file in another folder
You don't have to go to a file to read it. Give Get-Content the file's path, relative or absolute, the same way you gave one to Set-Location:
Get-Content day-3-mountains\trail-map.txt
Trail to the summit: 3 miles up.
The waterfall is halfway, on the left.
Commas give it several files at once, one after the other:
Get-Content day-1-beach\beach-notes.txt, day-2-old-town\market-notes.txt
The tide pool by the pier was full of tiny crabs.
The fruit stand at the market sells peaches for 50 cents each.
A hidden file reads like any other, once you know its name: Get-Content budget.txt prints Trip budget: $600. Spent so far: $412.
What makes a file readable
Get-Content reads text files: files of letters, numbers and spaces. The file extension, the letters after the last dot in the name, usually says what a file holds. .txt, .csv, .json, .log and .md are text, and print as you'd expect. A real photo (.jpg) or a program (.exe) is stored as numbers that aren't letters, and prints as a screen of gibberish; open those in the app made for them. (The trip's photos are empty pretend files, so they print nothing at all.)
A folder isn't a file, and PowerShell tells you what to use instead:
Get-Content day-1-beach
Get-Content: Unable to get content because it is a directory: 'C:\Users\Sam\terminal-practice\trip\day-1-beach'. Please use 'Get-ChildItem' instead.
Long files: more, and an editor
Some text files are long. PowerShell's own list of the software it's built with, ThirdPartyNotices.txt, is over 4,000 lines, and Get-Content would scroll all of them past in a second. Add | more (read "pipe more") to see one screen at a time:
Get-Content "$PSHOME\ThirdPartyNotices.txt" | more
$PSHOME is the folder PowerShell is installed in. Press Space for the next screen, Enter for one more line, and Q to stop. The |, a pipe, hands one command's output to the next; Module 3 is all about it.
When you want to scroll back, search, or change the file, open it in an editor instead. edit opens Microsoft Edit, a small editor that runs inside the terminal and comes with Windows 11:
edit itinerary.txt
Move with the arrow keys or the mouse, type to change the text, press Ctrl+S to save and Ctrl+Q to quit. If you'd rather have a window, notepad itinerary.txt opens the same file in Notepad: the terminal and the mouse, working on one file.
So, as a rule of thumb: a short file, Get-Content; a long one to read, | more; a file to change, an editor.
On a Mac or Linux, use / in paths; more works the same, and Microsoft Edit needs installing before edit works. Windows PowerShell 5.1 reads files the same way.
Try it in the terminal
Terminal Quest: The Lighthouse, goal 3: played right on the lesson page.
There's a welcome note waiting for you at the lighthouse. Read it, and use Tab so you type as little of its name as you can.
Check yourself
2 questions to try on the lesson page.
So what
Most of the files that make software and computers work are text: settings, logs, instructions. Reading them where you stand, without hunting for them in a window, is one of the most useful everyday skills the terminal gives you.