PowerShell: Read File

By Xah Lee. Date: . Last updated: .

Get-Content has alias of gc, cat, type .

Get-Content filepath

return the file content as a array of strings. Each element is a line.

Get-Content filepath -TotalCount n

return first n lines as a array of strings.

Get-Content filepath -tail n

return last n lines as a array of strings.

Get-Content filepath -raw

return whole file as one string.

Get-Content filepath -Encoding utf8

Use utf8 encoding.

Here's a alternative, using dotnet method:

[IO.File]::ReadAllText(filepath)

return the content of the file, as a string.

PowerShell: Input/Output