Get the content of the item at the specified location. Get-Content reads the content one line at a time and returns an object for each line.

      Get-Content [ [-path] | -literalPath ] string[]
         [-ReadCount Int64] [-TotalCount Int64]
            [-include string[]] [-exclude string[]] [-filter string]
               [-force] [-credential PSCredential]
                  [-Encoding] [-Delimiter String] [-Wait] [-Raw]
                     [-UseTransaction] [CommonParameters]

   -Path string
       The paths to the items from which content is to be retrieved.
       Wildcards are permitted.

   -LiteralPath string
       Like Path above, only the value is used exactly as typed.
       No characters are interpreted as wildcards. If the path includes any
       escape characters then enclose the path in single quotation marks.

   -TotalCount Int64
       Number of lines of content to retrieve. The default is -1 (all lines).

   -ReadCount Int64
       Send n lines of content through the pipeline at a time.
       The default is 1, this will emit each text line separately which is ideal when using the pipeline.
       A value of 0 will send all the content at once, use this when storing the text in a variable.
       A low value for ReadCount will return the first line quickly,
       To minimise the total time for the operation choose a high value (or 0).

   -Include string
       Retrieve only the specified items from the Path. 
       Wildcards are permitted. e.g. "*.txt"

   -Exclude string
       Omit the specified items from the Path (this qualifies the Path parameter)
       Wildcards are permitted. e.g. "*.log"

   -Filter string
       A filter in the provider's format or language. 
       The exact syntax of the filter (wildcard support etc) depends on the provider.
       Filters are more efficient than -include/-exclude, because the provider
       applies the filter when retrieving the objects, rather than having 
       PowerShell filter the objects after they are retrieved.

       Override restrictions that prevent the command from succeeding, apart
       from security settings. e.g. Force will create file path directories 
       or override a files read-only attribute, but will not change file permissions.

   -Credential PSCredential
       Present a user/password credential to validate access to the file.
       This is not yet supported in any Windows PowerShell core commands.

       Include the command in the active transaction.

   -Encoding CharSet  [Dynamic Parameter (FileSystem Only)]
       Encode in a specific character set:
           Unknown           Unknown or invalid. The data can be treated as binary.
           String            Use the encoding type for a string.                
           Unicode           UTF-16 format little-endian byte order.
           Byte              Encode characters as a sequence of bytes.
           BigEndianUnicode  UTF-16 format big-endian byte order.
           UTF8              UTF-8 format.
           UTF7              UTF-7 format.
           ASCII             ASCII (7-bit) character set.

   -Delimiter String  [Dynamic Parameter (FileSystem Only)]
      The delimiter to use when reading the file. The default is "\n" (end of line).

      Return multiple lines as a single string (PowerShell 3.0)
      In PowerShell 2.0 use the static method: [System.IO.File]::ReadAllText(string path)

   -Wait  [Dynamic Parameter (FileSystem Only)]
      Wait for content to be appended to the file.
      If content is appended, the appended content is returned. If the content is changed,
      the entire file is returned.  When waiting, Get-Content checks the file once each second until
      interrupted with CTRL+C.

Standard Aliases for Get-Content: cat, type, gc

Get-Content gets the content of the item at the location specified, such as the text in a file.

By default PowerShell will read the content one line at a time and return a collection of System.String objects.
Carriage returns (CR/LF) are stripped from the original file but when PowerShell displays a collection of strings, it will re-insert carriage returns so that the display will look just like the original file.

If you specify -raw, this behaviour is reversed and the entire file will be read at once.

Small files

If the file is small enough to be mounted in memory, then Select-String will be much faster, there is no need to Get-Content and then pipe into Select-String, you can just pipe it directly.

Large files /StreamReader

When working with a large file (or a lot of small files) a faster option is the .Net StreamReader

$file = new-object System.IO.StreamReader("C:\demo\large_file.txt")
try {
    while (($line = $file.ReadLine()) -ne $null) {
       # do something with $line here
finally {


Display the content of a file on the console (via the pipeline):

PS C:\> get-content -Path C:\myFile.txt

Get the first 50 lines from one file and write into another file:

PS C:\> get-content C:\Logs\BigLogfile.txt -totalcount 50 | set-content c:\Logs\top50.txt

Get the first 50 lines from one file and then uses array notation to get the last line (indicated by "-1") of the resulting set:

PS C:\> (get-content C:\Logs\Logfile.txt -totalcount 50)[-1]

Show the tail end of a log file in realtime:

PS C:\> Get-Content C:\Logs\Logfile.txt -Wait

Filter the log using a -match regular expression:

PS C:\> Get-Content C:\Logs\Logfile.txt -wait | where { $_ -match "WARNING" }

To return a single string for an entire file, several methods can be used:

PS C:\> $text = ((Get-Content -Path sample.txt -ReadCount 0) -join "`r`n") + "`r`n"

PS C:\> $text = Get-Content -Path sample.txt | Out-String

PS C:\> [System.Io.File]::ReadAllText("c:\demo\sample.txt")

PS C:\> $text = Get-Content -Path sample.txt -raw

“It is impossible to write ancient history because we do not have enough sources, and impossible to write modern history because we have far too many” ~ Charles Peguy

Related PowerShell Cmdlets

Add-Content - Add to the content of the item.
Set-Content - Set content in the item (specific location).
Clear-Content - Remove content from a file /item.
Get-Item - Get a file object or get a registry (or other namespace) object.
Replace - Search and Replace in strings and files.
help about_namespace.

Copyright © 1999-2024
Some rights reserved