Powershell

How to Quickly Display Files With PowerShell Cat

The cmdlet “Cat” is Linux based and helps to merge, read, concatenate, and display files in the console. However, this cmdlet is not supported in PowerShell. But PowerShell has its alternative “Get-Content” cmdlet. It can execute all the tasks that the “Cat” cmdlet can. Moreover, it gets one or more items from the specified location by the user.

The following blog will cover the alternatives of the cmdlet “Cat”.

How to Quickly Display Files With PowerShell Cat?

The files in PowerShell can be displayed with the aid of the “Cat” cmdlet alternative which is “Get-Content”. Multiple examples of the “Get-Content” cmdlet are illustrated below.

Example 1: Use the “Get-Content” Cmdlet to Read a File

The following example will retrieve or read the content of the specified file:

Get-Content "C:\Doc\File.txt"

According to the above code, first add the “Get-Content” cmdlet and specify the address of the file to be read:

Example 2: Use the “Get-Content” Cmdlet to Merge or Concatenate Two Files

The example below will help join or merge two files together with the aid of “Get-Content” cmdlet:

Get-Content C:\Doc\File_1.txt, C:\Doc\File_2.txt | Out-File C:\Doc\New_File.txt

In this stated code below:

  • First, add the “Get-Content” cmdlet followed by the two file paths separated by commas.
  • After that, specify the pipeline “|”, define the “Out-File” cmdlet, and assign the target file path:

Let’s verify whether the files were merged or not by using the command below:

Get-Content C:\Doc\New_File.txt

Example 3: Use the “Get-Content” Cmdlet to Display the First Four Lines of Text

The following command will get the initial four lines of the text file:

Get-Content C:\Doc\File.txt -TotalCount 4

In the code above, add the “-TotalCount” parameter and assign the value “4” to it:

Example 4: Use the “Get-Content” Cmdlet to Display the Last Four Lines of Text

This example will retrieve the four bottom lines of the text file:

Get-Content C:\Doc\File.txt -Tail 4

In the above code, simply add the “-Tail” parameter and assign the value “4” to it:

That was all about displaying files using PowerShell.

Conclusion

The “Cat” cmdlet is a Linux base cmdlet not supported in PowerShell. However, PowerShell has its alternative “Get-Content” cmdlet. It performs all the operations that the “Cat” cmdlet can. These operations include merging, concatenating, reading, or displaying text files. This blog has elaborated on displaying the files with PowerShell.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.