PowerShell: File Read

Read a file and stream to stdout

PS C:\> Get-Content file.txt
PS C:\> [System.IO.File]::ReadAllLines("file.txt")

Read a file line by line and save it to a variable

PS C:\> $file = Get-Content file.txt
PS C:\> $file[0]

PS C:\> $file = [System.IO.File]::ReadAllLines("file.txt")
PS C:\> $file[0]

Read a file as Byte[]

#Powershell 5
PS C:\> $file = Get-Content file.txt -Encoding Byte

#Powershell 6 and above
PS C:\> $file = Get-Content file.txt -AsByteStream

PS C:\> $file = [System.IO.File]::ReadAllBytes("file.txt")

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *