PowerShell: Read File
Get-Content
Get-Content
has alias ofgc
,cat
,type
.
Get-Content filepath
-
return the file content as a array of strings. Each element is a line.
Get-Content filepath -raw
-
return whole file as one string.
Get-Content filepath -Encoding utf8
-
Use UTF8 encoding. Unicode: UTF-8 Encoding
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.
Dotnet Method ReadAllText
Here's a alternative, using dotnet method:
[IO.File]::ReadAllText(filepath)
-
return the content of the file, as a string.