PowerShell: Read File

By Xah Lee. Date: . Last updated: .
Get-Content (alias gc)
open and read a file, return a string or array of string, or bytes.
gc filePath
print the file content.
gc filePath -TotalCount n
get first n lines.
gc filePath -tail n
get last n lines.
gc filePath -raw
whole file as one string
gc filePath -Encoding utf8
Use utf8 encoding.

Note: Get-Content is line based. It returns a array of strings, except when -raw or -byte is used.

Here's a alternative, using .NET method:

[IO.File]::ReadAllText(file_path)
return the content of the file, as a string.

PowerShell

How-to

Advanced

Script Examples