Loading...
X

Analogs of cat, tail, head and wc in PowerShell

How to pass the contents of a file to a cmdlet in PowerShell

In this article, we will look at how to get the contents of a file and send it to a cmdlet for processing or for display on the screen.

In Bash on Linux, when you need to pass data from a file, commands either work with standard input or the path to the file is specified using an option. In PowerShell, cmdlets can also accept data from standard input, for example:

Get-Content -Path C:\Domain01.txt | Restart-Computer

As you may have already understood from the command above, the Get-Content cmdlet is the equivalent of cat in PowerShell. If you need to pass the contents of a file to an option, then use the construction, an example of which is shown in the following command:

Add-Computer -ComputerName (Get-Content Servers.txt) -DomainName Domain02 -Credential Domain02\Admin02 -Options Win9xUpgrade -Restart

For subsequent examples, create a text file in which you write a list of folders and files in the current directory:

Get-ChildItem > current_dir.txt

Analog of cat, tail and head in PowerShell

Linux has several commands for reading a file:

  • cat – reads the entire file
  • tail – reads the specified number of lines (default 10) from the end of the file
  • head – reads the specified number of lines (default 10) from the beginning of the file

For all this, PowerShell uses the Get-Content cmdlet.

Example of reading the contents of the file current_dir.txt (the contents will be piped or, if there are no other cmdlets, outputted to standard output, i.e., to the screen):

Get-Content current_dir.txt

Note: In the Windows version of PowerShell, you can use the cat command as an alias for Get-Content. In the Linux version of PowerShell, if you try to use cat in the pwsh console, the Linux utility will be used.

The Get-Content cmdlet gets the contents of an element in files, but it can also get the contents of a function. For files, the contents are read one line at a time and return a set of objects, each of which is a line of content.

How to read a file and save it to a variable

How can I save the contents of a file to a file instead of printing it to the console? That is, how to make an analogue of the following Bash command:

a="`cat FILE`"
echo "$a"

The following command will read the file current_dir.txt and save its contents to the variable $dir:

$dir = Get-Content current_dir.txt

How to read and merge the contents of several files

Let's consider how to make an analogue of the following command:

cat file1.txt file2.txt

To simultaneously read and merge the output of several files, pass the file names as an array:

Get-Content -Path @('file1.txt', 'file2.txt')

Since the array can be created simply by listing its values ​​​​separated by commas, without using the “@” symbol, and since the -Path option can be omitted, you can shorten the entry a little:

Get-Content 'file1.txt', 'file2.txt'

In short, to read several files at once in Get-Content, list the file names separated by commas.

-Path option and wildcards with Get-Content

You can use the -Path option to specify a file, but you can also omit it:

Get-Content -Path 'C:\Server\bin\PHP\php.ini'

You can use wildcards:

Get-Content -Path 'C:\Server\bin\PHP\php.*'

The path must specify items, not containers. For example, you can specify a path to one or more files, but not a directory.

That is, the commands discussed in this section are analogs of

cat file*
cat C:\Server\bin\PHP\php.*
cat C:\Server\bin\PHP\*.conf

How to read a certain number of lines from the beginning of a file

Let's consider how to make an analog of the following command:

head -n 10 current_dir.txt

Using the -TotalCount option (its aliases are -First and -Head), you can specify the number of lines that should be read from the beginning of the file.

The following command will read the first 13 lines of the file current_dir.txt:

Get-Content current_dir.txt -TotalCount 10

How to read a certain number of lines from the end of a file

Let's look at how to make an analogue of the following command:

tail -n 10 current_dir.txt

With the -Tail option, you can read the last lines of a file, for example:

Get-Content current_dir.txt -Tail 10

How to find out how many lines have been read

Let's look at how to make an analogue of the following command:

cat current_dir.txt | wc -l

The Get-Content cmdlet returns an array of lines, so you can use array methods, for example, the following commands will show the number of lines in the file current_dir.txt:

(Get-Content current_dir.txt).Count
(Get-Content current_dir.txt).Length

You can also use the Measure-Object cmdlet to count the number of lines:

Get-Content current_dir.txt | Measure-Object -Line | select Lines

Note that these commands returned different numbers of rows. The first command returned the total number of rows including empty ones, while the second command counted only non-empty rows.


Leave Your Observation

Your email address will not be published. Required fields are marked *