Get-Content

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.

Syntax
      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]

Key
   -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, a value of 0 sends all the content at once. 
        
       A low value for ReadCount will return the first line quickly,
       to minimise the total time for the operation choose a high value.

   -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.

   -Force
       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.

   -UseTransaction
       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).

   -Raw
      Return multiple lines as a single string (PowerShell 3.0)

   -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.

   CommonParameters:
       -Verbose, -Debug, -ErrorAction, -ErrorVariable, -WarningAction, -WarningVariable,
       -OutBuffer -OutVariable.

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. It reads the content one line at a time and by default returns an object for each line.

Examples

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:

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 SS64.com 1999-2013
Some rights reserved