PowerShell Get-Content -Raw: read a whole file or individual lines

  • Published: 2026.10.13
  • Updated: 2026.10.13
  • Windows

You read a text file in PowerShell, but a line count turns out to be a character count, or the value you pass to another command becomes an array. The text may look identical in the console even though the value stored in your variable has a different shape.

Get-Content normally reads a file line by line. Add -Raw to get the whole file as one string, including its newline characters. Choosing the form your next operation needs makes the rest of the script easier to reason about. These examples use a small UTF-8 text file on Windows and work in Windows PowerShell 5.1 and PowerShell 7.


Create a sample text file

Use a new working folder, separate from your own files. This example creates get-content-demo and writes two lines to sample[1].txt. If that folder already exists, choose another name and run the commands somewhere they will not overwrite existing files.

New-Item -ItemType Directory -Path .\get-content-demo -ErrorAction Stop | Out-Null
Set-Location -LiteralPath .\get-content-demo
Set-Content -LiteralPath '.\sample[1].txt' -Value @('alpha', 'beta') -Encoding UTF8

Square brackets can be interpreted as wildcard syntax with -Path. Here, -LiteralPath uses the filename exactly as written. To check whether a file exists first, see Test-Path and LiteralPath for file checks.

Read and process individual lines

Leave out -Raw when you want to work with individual lines. Wrapping the result in @(...) keeps it as an array, so Count gives the line count even when the file contains one line or no lines.

$lines = @(Get-Content -LiteralPath '.\sample[1].txt' -Encoding UTF8)
$lines.Count
$lines[0]

The output shows two array elements and alpha as the first line. Each element excludes the line-ending characters.

2
alpha

Without @(...), assigning the result from a one-line file gives you a single string. Its Length is a character count, and [0] is its first character rather than its first line. If a script works for several lines but changes behavior for one line, check the type of value it receives.

Read the whole file as one string

Use -Raw when the next operation needs the complete text. This is useful for searching within a string or passing a JSON document to a parser: you do not have to join an array of lines yourself.

$text = Get-Content -LiteralPath '.\sample[1].txt' -Encoding UTF8 -Raw
$text.GetType().Name
$text.Length
$text.Contains("`r`n")

For the sample file created above on Windows, the result is:

String
13
True

The five characters in alpha and four in beta, plus two CRLF sequences, make 13 characters. The final newline remains in the string. A file with LF-only line endings has a different length, so string Length is not a reliable line count.

Read just the lines you need

-TotalCount reads from the beginning of a file, while -Tail reads from the end. These commands display the first and last line, respectively.

Get-Content -LiteralPath '.\sample[1].txt' -Encoding UTF8 -TotalCount 1
Get-Content -LiteralPath '.\sample[1].txt' -Encoding UTF8 -Tail 1
alpha
beta

You cannot combine either of these options with -Raw. Use -Tail on its own for the end of a log, and choose -Raw when you need the complete text as one string.

Handle empty files and read errors

Reading an empty file with -Raw returns $null. If the next operation needs an empty string instead, you can convert the result to [string]. With line-by-line reading, @(...) produces an array whose Count is zero.

A missing file or a permissions problem produces an error rather than the result of an empty file. Add -ErrorAction Stop when subsequent processing should stop, and use try/catch where appropriate. Treat a failed read separately from successfully reading empty content.

If characters appear corrupted, match -Encoding to the file’s actual encoding. Windows PowerShell 5.1 and PowerShell 7 have different defaults. Explicitly specifying UTF-8, as in these examples, makes reading consistent when the file is known to be UTF-8. Specifying UTF-8 for a Shift_JIS file does not convert its contents correctly.

Limit what you read from large files

-Raw loads the whole file into a string in memory, which can be costly for a large log or dataset. If you only need the beginning or end, use -TotalCount or -Tail. For per-line processing, use line-by-line reading. Assigning all the lines to a variable still retains those values in memory, so consider both how you read the file and how you process the result.

For the full parameter reference, see Get-Content in Windows PowerShell 5.1 and Get-Content in PowerShell 7.

このブラウザからの自分のアクセスを解析から除外できます。