Get-Content

Updated: August 9, 2015

Get-Content

Gets the content of the item at the specified location.

Aliases

The following abbreviations are aliases for this cmdlet: 

  • cat, gc, type

Syntax

Parameter Set: Path
Get-Content [-Path] <String[]> [-Credential <PSCredential> ] [-Delimiter <System.String> ] [-Encoding <Microsoft.PowerShell.Commands.FileSystemCmdletProviderEncoding> {Unknown | String | Unicode | Byte | BigEndianUnicode | UTF8 | UTF7 | UTF32 | Ascii | Default | Oem | BigEndianUTF32} ] [-Exclude <String[]> ] [-Filter <String> ] [-Force] [-Include <String[]> ] [-Raw] [-ReadCount <Int64> ] [-Stream <System.String> ] [-Tail <Int32> ] [-TotalCount <Int64> ] [-Wait] [-UseTransaction] [ <CommonParameters>]

Parameter Set: LiteralPath
Get-Content -LiteralPath <String[]> [-Credential <PSCredential> ] [-Delimiter <System.String> ] [-Encoding <Microsoft.PowerShell.Commands.FileSystemCmdletProviderEncoding> {Unknown | String | Unicode | Byte | BigEndianUnicode | UTF8 | UTF7 | UTF32 | Ascii | Default | Oem | BigEndianUTF32} ] [-Exclude <String[]> ] [-Filter <String> ] [-Force] [-Include <String[]> ] [-Raw] [-ReadCount <Int64> ] [-Stream <System.String> ] [-Tail <Int32> ] [-TotalCount <Int64> ] [-Wait] [-UseTransaction] [ <CommonParameters>]




Detailed Description

The Get-Content cmdlet gets the content of the item at the location specified by the path, such as the text in a file. It reads the content one line at a time and returns a collection of objects, each of which represents a line of content.

Beginning in Windows PowerShell 3.0, this cmdlet can also get a specified number of lines from the beginning or end of an item.

Parameters

-Credential<PSCredential>

Specifies a user account that has permission to perform this action. The default is the current user.

Type a user name, such as User01 or Domain01\User01, or enter a PSCredential object, such as one generated by the Get-Credential cmdlet. If you type a user name, you will be prompted for a password.

This parameter is not supported by any providers that are installed with Windows PowerShell.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

true (ByPropertyName)

Accept Wildcard Characters?

false

-Delimiter<System.String>

This parameter is not supported by any providers that are installed with Windows PowerShell.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Encoding<Microsoft.PowerShell.Commands.FileSystemCmdletProviderEncoding>

This parameter is not supported by any providers that are installed with Windows PowerShell.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Exclude<String[]>

Specifies, as a string array, the item or items that this cmdlet omits when performing the operation. The value of this parameter qualifies the Path parameter. Enter a path element or pattern, such as *.txt. Wildcards are permitted.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Filter<String>

Specifies a filter in the provider's format or language. The value of this parameter qualifies the Path parameter. The syntax of the filter, including the use of wildcards, depends on the provider. Filters are more efficient than other parameters, because the provider applies them when this cmdlet gets the objects, rather than having Windows PowerShell filter the objects after they are retrieved.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Force

Forces the command to run without asking for user confirmation.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Include<String[]>

Specifies, as a string array, the item or items that this cmdlet includes in the operation. The value of this parameter qualifies the Path parameter. Enter a path element or pattern, such as *.txt. Wildcards are permitted.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-LiteralPath<String[]>

Specifies the path to an item. Unlike the Path parameter, the value of LiteralPath is used exactly as it is typed. No characters are interpreted as wildcards. If the path includes escape characters, enclose it in single quotation marks. Single quotation marks tell Windows PowerShell not to interpret any characters as escape sequences.


Aliases

PSPath

Required?

true

Position?

named

Default Value

none

Accept Pipeline Input?

true(ByPropertyName)

Accept Wildcard Characters?

false

-Path<String[]>

Specifies the path to an item. Get-Content gets the content of the item. Wildcards are permitted.


Aliases

none

Required?

true

Position?

1

Default Value

none

Accept Pipeline Input?

true (ByPropertyName)

Accept Wildcard Characters?

false

-Raw

This parameter is not supported by any providers that are installed with Windows PowerShell.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-ReadCount<Int64>

Specifies how many lines of content are sent through the pipeline at a time. The default value is 1. A value of 0 (zero) sends all of the content at one time.

This parameter does not change the content displayed, but it does affect the time it takes to display the content. As the value of ReadCount increases, the time it takes to return the first line increases, but the total time for the operation decreases. This can make a perceptible difference in very large items.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

true (ByPropertyName)

Accept Wildcard Characters?

false

-Stream<System.String>

This parameter is not supported by any providers that are installed with Windows PowerShell.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-TotalCount<Int64>

Specifies the number of lines from the beginning of a file or other item. The default is -1 (all lines).

You can use the TotalCount parameter name or its aliases, First or Head.


Aliases

First, Head

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

true (ByPropertyName)

Accept Wildcard Characters?

false

-Tail<Int32>

Specifies the number of lines from the end of a file or other item.

This parameter was introduced in Windows PowerShell 3.0.

You can use the Tail parameter name or its alias, Last.


Aliases

Last

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

true (ByPropertyName)

Accept Wildcard Characters?

false

-Wait

This parameter is not supported by any providers that are installed with Windows PowerShell.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-UseTransaction

Includes the command in the active transaction. This parameter is valid only when a transaction is in progress. For more information, see  about_Transactions


Required?

false

Position?

named

Default Value

false

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

<CommonParameters>

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see    about_CommonParameters.

Inputs

The input type is the type of the objects that you can pipe to the cmdlet.

  • None

    You cannot pipe input to this cmdlet.


Outputs

The output type is the type of the objects that the cmdlet emits.

  • System.Byte, System.String

    This cmdlet returns strings or bytes. The output type depends upon the content that it gets.


Notes

  • This cmdlet is designed to work with the data exposed by any provider. To get the providers in your session, use the Get-PSProvider cmdlet. For more information, see about_Providers(http://go.microsoft.com/fwlink/?LinkID=113250).

Examples

Example 1: Get the content of a text file

This command gets the content of the Chapter1.txt file. It uses the Path parameter to specify the name of the item. Get-Content passes the content down the pipeline, but because there are no other pipeline elements, the content is formatted by default and displayed at the command line.


PS C:\>Get-Content -Path "C:\Chapters\Chapter1.txt"

Example 2: Get the first 50 lines from a text file and store the results in another file

This command gets the first 50 lines of the Log060912.txt file and stores them in the Sample.txt file. The command uses the Get-Content cmdlet to get the text in the file. (The name of Path parameter, which is optional, is omitted.) The TotalCount parameter limits the content retrieved to the first 50 lines. The pipeline operator (|) sends the result to the Set-Content cmdlet, which places it in the Sample.txt file.


PS C:\>Get-Content "C:\Logs\Log060912.txt" -TotalCount 50 | Set-Content "Sample.txt"

Example 3: Get the fifth line of a text file

This command gets the fifth line of the Cmdlets.txt text file. It uses the TotalCount parameter to get the first five lines and then uses array notation to get the last line (indicated by "-1") of the resulting set.


PS C:\>(Get-Content Cmdlets.txt -TotalCount 5)[-1]

Example 4: Get the first and last line of a text file

This command gets the first and last lines of each text file in the current directory. The command uses the Tail parameter and the Head alias of the TotalCount parameter


PS C:\>dir .\*.txt | ForEach {Get-Content $_ -Head 1; Get-Content $_ -Tail 1}

Related topics

Community Additions

ADD
Show: