PowerShell: Read File

By Xah Lee. Date: . Last updated: .

Get-Content has alias of gc

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 .NET method:

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

PowerShell: Input/Output


PowerShell in Depth

Path

Pipe

Comment

Value Types

String

Variable

Boolean

Conditional

Data Structure

Loop and Iteration

Input/Output

Function

Profile and Script

Script Examples

Misc