How to read file contents in PowerShell. How to save file contents to a variable
September 18, 2024
Table of contents
- How to read file contents and display them on the screen
- How to read a file and save it to a variable
- How to read multiple files with a single Get-Content command
- Using wildcards with file names in Get-Content
- How to read a specified number of lines from the beginning of a file
- How to read a certain number of lines from the end of a file
- How to find out how many lines have been read
- How to print only specific lines of a file
- How to get the entire file as a single line
- How to get the contents of a file as a byte stream
- Choosing a new delimiter
- Specifying a file encoding
- Monitoring a file for new lines
How to read file contents and display them on the screen
The Get-Content cmdlet is used to read files. The name of the file to be read is specified after the -Path option – this option can be omitted. That is, the following two commands are equivalent:
Get-Content FILE Get-Content -Path FILE
For example:
Get-Content file1.txt
The read contents can be passed to another cmdlet or assigned to a variable. An example of reading file contents using Get-Content and passing it to another cmdlet for subsequent processing:
Get-Content file1.txt | Select-String '='
If none of this was done, the file contents will simply be displayed on the screen.
How to read a file and save it to a variable
The following command will read the file file1.txt and save its contents to the variable $file1:
$file1 = Get-Content file1.txt
How to read multiple files with a single Get-Content command
In Windows, the Get-Content cmdlet has the alias cat, which hints that the Get-Content cmdlet is meant as an analogue of cat in PowerShell. If you recall the purpose of the cat command, it is “concatenate files” (hence the name of the utility), that is, combining files. Of course, many specify only one file as the cat option and use this utility as a convenient way to display or write the contents of a single file to a variable.
See also: Analogs of cat, tail, head and wc in PowerShell
If you specify more than one file after the Get-Content cmdlet, this will cause an error. If you use the -Path option more than once, this will cause an error again.
The solution to this problem is to pass the list of files as an array.
If you are trying to find how to read and merge the contents of multiple files, then simply use an array of file names:
Get-Content -Path @('file1.txt', 'file2.txt')
Since the array can be created by simply listing its values separated by commas, without using the “@” symbol, and since the -Path option can be omitted, you can shorten the command a bit:
Get-Content 'file1.txt', 'file2.txt'
Since PowerShell correctly handles strings even if they are not placed in quotes, the command can be written as follows – this will also work, but only if the file names do not contain spaces or other special characters:
Get-Content file1.txt, file2.txt
All three of the last commands will give identical results. That is, it is enough to list the file names separated by commas. In my opinion, it is better to get into the habit of using quotes so that you don't have problems regardless of spaces and special characters in file names.
Using wildcards with file names in Get-Content
You can read multiple files at once (or a single file whose name is partially known) by using wildcards in file names. For example:
Get-Content file* Get-Content *.txt
You can combine arrays of file names and wildcards, for example:
Get-Content file*,DICTIONARY
How to read a specified number of lines from the beginning of a file
With the -TotalCount option (also aliased as -First and -Head), you can specify the number of lines to read from the beginning of a file.
The following command will read the first 13 lines of file1.txt:
Get-Content file1.txt -TotalCount 13
How to read a certain number of lines from the end of a file
With the -Tail option, you can read the last lines of a file, for example:
Get-Content file1.txt -Tail 10
How to find out how many lines have been read
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 file1.txt:
(Get-Content file1.txt).Count (Get-Content file1.txt).Length
You can also use the Measure-Object cmdlet to count the number of lines:
Get-Content file1.txt | Measure-Object -Line | select Lines
Note that these commands returned different numbers of lines. The first command returned the total number of lines including empty lines, while the second command counted only non-empty lines.
How to print only specific lines of a file
You can print only specific lines by listing their numbers separated by commas using the following syntax:
(Get-Content file1.txt)[3,4,5]
If you saved the file contents to a variable, you can use the same syntax on the variable:
$file = Get-Content file1.txt $file[3,4,5]
Remember that the array numbers start at 0, so in the previous examples, although the numbers 3, 4, and 5 were specified, the lines printed were actually lines 4, 5, and 6.
How to get the entire file as a single line
By default, without the -Raw option, the contents are returned as an array of lines separated by newlines.
If you add the -Raw option, the file read command ignores newline characters and returns the entire contents of the file on a single line, preserving newline characters. By default, newline characters in a file are used as delimiters to break the input into an array of lines.
Example command:
Get-Content file1.txt -Raw
If you are outputting the file to the screen, this will not affect its display.
How to get the contents of a file as a byte stream
The -AsByteStream option specifies that the contents should be read as a byte stream.
A warning is generated when using the -AsByteStream option with the -Encoding option. The -AsByteStream option ignores any encoding, and the output is returned as a byte stream.
When reading and writing binary files, use the -AsByteStream option and a value of 0 for the -ReadCount option. A -ReadCount value of 0 reads the entire file in a single read operation. The default -ReadCount value of 1 reads one byte in each read operation and converts each byte into a separate object. Piping single-byte output to Set-Content results in errors unless you use the -AsByteStream parameter with Set-Content.
This example shows how to get the contents of a file as [byte[]] as a single object.
$byteArray = Get-Content -Path file1.txt -AsByteStream -Raw
The -Raw parameter ensures that the bytes are returned as [System.Byte[]]. If the -Raw parameter were missing, the return value is a byte stream that PowerShell interprets as [System.Object[]].
Choosing a new delimiter
The -Delimiter option lets you specify the delimiter that Get-Content will use to split the file into objects while reading. The default is \n, the end-of-line character. When reading a text file, Get-Content returns a collection of string objects, each terminated by a line terminator. When you enter a delimiter that is not in the file, Get-Content returns the entire file as a single, unsplit object.
You can use this option to split a large file into smaller files by specifying the file separator as the delimiter. The delimiter is preserved (not removed) and becomes the last element in each section of the file.
Specifying a file encoding
The -Encoding option specifies the encoding type of the target file. The default is utf8NoBOM.
The following are valid values for this option:
- ascii: Uses the ASCII character set (7-bit) encoding.
- ansi: Uses the encoding for the ANSI code page of the current culture. This option was added in PowerShell 7.4.
- bigendianunicode: Encodes in big-endian UTF-16 format.
- bigendianutf32: Encodes as little-endian UTF-32.
- oem: Uses the default encoding for MS-DOS and console programs.
- unicode: Encodes as little-endian UTF-16.
- utf7: Encodes as UTF-7.
- utf8: Encodes as UTF-8.
- utf8BOM: Encodes as UTF-8 with a byte order mark (BOM).
- utf8NoBOM: Encodes as UTF-8 without a byte order mark (BOM).
- utf32: Encodes as UTF-32.
Starting with PowerShell 6.2, the -Encoding parameter also accepts numeric identifiers of registered code pages (e.g., -Encoding 1251) or string names of registered code pages (e.g., -Encoding "windows-1251"). For more information, see the .NET documentation for Encoding.CodePage: https://learn.microsoft.com/en-us/dotnet/api/system.text.encoding.codepage
Starting with PowerShell 7.4, you can use the Ansi value for the -Encoding parameter to pass the numeric identifier for the current ANSI code page of a culture without having to specify it manually.
Monitoring a file for new lines
The -Wait option causes the cmdlet to wait indefinitely, keeping the file open until the command is interrupted. While waiting, Get-Content checks the file once per second and prints out new lines if any.
Get-Content file1.txt -Wait
When a line is deleted, the entire file is re-displayed.
When used with the -TotalCount parameter, Get-Content waits until the specified number of lines are available in the specified file. For example, if you specify -TotalCount equal to 10 and the file already contains 10 or more lines, Get-Content returns 10 lines and exits. If the file has fewer than 10 lines, Get-Content displays each line as it is received, but waits until the tenth line is available before exiting.
You can interrupt -Wait by pressing Ctrl+C. Deleting a file causes an error, which also interrupts the cmdlet.
-Wait cannot be combined with -Raw.
Related articles:
- How to monitor new lines in a file in PowerShell (65.9%)
- Error “The '<' operator is reserved for future use.” (SOLVED) (59.3%)
- How to find out the current directory in the Windows command line. Analog of pwd in PowerShell (57.6%)
- How to clear the terminal window in PowerShell. Analog of clear (57.6%)
- An analogue of cd and chdir in PowerShell. How to change the current working folder in PowerShell (57.6%)
- How to prevent Tor users from viewing or commenting on a WordPress site (RANDOM - 50%)