If you‘re used to using the handy cat command in Linux to quickly view files, you may be wondering how to mimic that functionality in PowerShell. Have no fear – the Get-Content cmdlet provides all the core features of cat directly within PowerShell!
In this guide, we‘ll explore the various ways to display file contents quickly using Get-Content. Whether you need to view an entire text file, see just the first or last few lines, or concatenate multiple files, Get-Content has you covered. Read on to master this indispensable command for every PowerShell user‘s toolkit.
Cat Command Capabilities
Let‘s first do a quick rundown of the useful capabilities provided by the cat command in Linux:
- Display an entire text file:
cat file.txt - Output multiple files together:
cat file1.txt file2.txt - Show first 10 lines:
cat file.txt -n 10 - Show last 10 lines:
cat file.txt -n -10 - Number all output lines:
cat -n file.txt - Suppress empty blank lines:
cat -s file.txt
cat is one of the most ubiquitous Linux commands – used billions of times daily across systems! But it‘s not built into PowerShell. Thankfully, we‘ve got Get-Content to save the day.
Quickly Displaying Files in PowerShell
The goal of this guide is to learn how Get-Content provides similar functionality to cat for quickly viewing files right within the PowerShell console.
Whether you need to inspect log files, peek at CSV data, or concatenate text – Get-Content is the go-to tool for any PowerShell user. Let‘s check it out!
Get-Content Overview
The Get-Content cmdlet outputs the contents of a specified file directly in the console. Here are some key features:
- Works on text files (.txt) and CSVs.
- Main parameter is
-Pathto specify the file. - Can output the entire file or just first/last lines.
- Concatenate multiple files into one output.
- Similar syntax to
catbut with PowerShell style.
Now let‘s see Get-Content in action with some real-world examples!
Display a Text File
The most straightforward use of Get-Content is to display an entire text file, just like cat in Linux:
Get-Content "C:\Doc\File.txt"

Pass the file path enclosed in quotes to Get-Content and it will dump the contents to screen – easy as that!
Some things to note with the basic display:
- The file must exist at the specified path or you‘ll get an error.
- Output will include empty blank lines between paragraphs.
- No numbering of lines like
cat -n.
Now let‘s look at displaying just parts of a file…
Show First N Lines
Often you only need to see the beginning of a text file. Just like cat -n 10, Get-Content can display only the first few lines by using the -TotalCount parameter:
Get-Content "C:\Doc\File.txt" -TotalCount 4

This outputs only the first 4 lines. -TotalCount accepts any positive integer value.
Some useful examples:
# First 15 lines
Get-Content log.txt -TotalCount 15
# First 100 lines
Get-Content report.csv -TotalCount 100
Display Last Lines of a File
To view just the end of a text file, use the -Tail parameter instead of -TotalCount:
Get-Content "C:\Doc\File.txt" -Tail 4

This will output only the last 4 lines of the file. Again, -Tail takes any positive integer value:
# Last 10 lines
Get-Content log.txt -Tail 10
# Last 50 lines
Get-Content report.csv -Tail 50
-TotalCount and -Tail allow displaying a subset of the file lines quickly without loading the entire contents.
Concatenate Multiple Files
A common use of cat is combining multiple files into a single output. Get-Content can handle this as well using the pipeline:
Get-Content File1.txt, File2.txt | Out-File Combined.txt
To concatenate, separate the file paths with commas rather than spaces. Then pipe the output to Out-File to write the joined contents to a new file.
Some examples combining different file types:
Get-Content report.csv, data.json | Out-File combined.txt
Get-Content *.log | Out-File logs.txt
The pipeline makes combining files a breeze!
Get-Content vs. Cat Comparison
Now that we‘ve seen Get-Content in action, let‘s compare syntax directly to the cat command:
| Task | Linux Cat | PowerShell Get-Content |
|---|---|---|
| Display file | cat file.txt |
Get-Content file.txt |
| First 10 lines | cat file.txt -n 10 |
Get-Content file.txt -TotalCount 10 |
| Last 10 lines | cat file.txt -n -10 |
Get-Content file.txt -Tail 10 |
| Concatenate files | cat file1.txt file2.txt > combined.txt |
Get-Content file1.txt, file2.txt \| Out-File combined.txt |
| Number output lines | cat -n file.txt |
Not directly supported |
As shown in the table, Get-Content mirrors cat very closely with similar display functionality. The parameters are analogous but follow standard PowerShell naming like -TotalCount rather than -n.
Overall, Get-Content provides native support for the core file handling scenarios of cat within the PowerShell ecosystem.
Get-Content in Action
Beyond basic displays, Get-Content really shines when combining with other PowerShell commands through piping.
For example, let‘s extract just the IP addresses from a log file:
Get-Content .\server.log | Select-String -Pattern "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
The file contents are passed to Select-String which filters for the regex pattern.
Here are some other ways to leverage Get-Content:
-
Search files with
Select-String:Get-Content .\access.log | Select-String "404" -
Format CSV files for readability:
Get-Content .\data.csv | ConvertFrom-CSV | Format-Table -
Parse JSON data:
Get-Content .\config.json | ConvertFrom-Json -
Count word frequency:
Get-Content .\report.txt | Group-Object -NoElement | Sort-Object Count -Descending
The ability to pipe the file contents to other commands is a key advantage of Get-Content over cat.
Working with Large Files
When dealing with very large text files, Get-Content will load the entire contents into memory before displaying the output. This can cause slow performance and high memory usage.
To work efficiently with large files:
-
Use the
-ReadCountparameter to process in chunks rather than all at once:Get-Content .\big_file.txt -ReadCount 1000 -
Avoid unnecessary direct output. Instead, pipe to commands accepting stream input:
Get-Content .\big_file.txt | Select-String "404" -
For regex matches against large files, use
Select-Stringdirectly rather thanGet-Content:Select-String "404" .\big_file.txt
With these best practices, Get-Content can handle large file processing without issue.
Troubleshooting Common Errors
When first getting started with Get-Content, you may run into some common errors like:
File not found:
Get-Content : Cannot find path ‘C:\missing.txt‘ because it does not exist.
Be sure to check the path case and spelling. PowerShell is case-sensitive for file paths!
Access denied:
Get-Content : Access to the path ‘C:\Program Files\logs.txt‘ is denied.
You need read permissions for the file path specified. Run PowerShell as administrator if accessing protected folders.
Out of memory exception:
OutOfMemoryException: Exception of type ‘System.OutOfMemoryException‘
Use the -ReadCount parameter as mentioned above when working with extremely large files.
Following the recommended practices will help avoid these common mistakes.
Summary of Get-Content
In summary, Get-Content provides similar display capabilities as the ubiquitous cat command but directly within PowerShell:
-
Display full text and CSV files in the console.
-
Read only first or last lines using
-TotalCountand-Tail. -
Concatenate multiple files using the pipeline.
-
Follows similar usage patterns to
catwith native PowerShell parameters. -
Enables further processing through piping to other PowerShell commands.
-
Avoid common errors like missing files and memory issues.
For any PowerShell user familiar with Linux environments, Get-Content will be an indispensable tool in your toolkit. It fulfills all the quick viewing and concatenation scenarios of cat while enabling deeper integration with PowerShell workflows.
I hope this guide has fully prepared you to leverage Get-Content like a pro! Let me know if you have any other questions.



