Share via


Get-Content

Applies To: Windows PowerShell 2.0

Gets the content of the item at the specified location.

Syntax

Get-Content [-LiteralPath] <string[]> [-Credential <PSCredential>] [-Exclude <string[]>] [-Filter <string>] [-Force] [-Include <string[]>] [-ReadCount <Int64>] [-TotalCount <Int64>] [-UseTransaction] [<CommonParameters>]

Get-Content [-Path] <string[]> [-Credential <PSCredential>] [-Exclude <string[]>] [-Filter <string>] [-Force] [-Include <string[]>] [-ReadCount <Int64>] [-TotalCount <Int64>] [-UseTransaction] [<CommonParameters>]

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.

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.

Required?

false

Position?

named

Default Value

Accept Pipeline Input?

true (ByPropertyName)

Accept Wildcard Characters?

false

-Exclude <string[]>

Omits the specified items. The value of this parameter qualifies the Path parameter. Enter a path element or pattern, such as "*.txt". Wildcards are permitted.

Required?

false

Position?

named

Default Value

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 retrieving the objects, rather than having Windows PowerShell filter the objects after they are retrieved.

Required?

false

Position?

named

Default Value

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Force

Overrides restrictions that prevent the command from succeeding, just so the changes do not compromise security. The restrictions that the Force parameter overrides varies among providers. For more information, see help for the provider and about_Providers.

Required?

false

Position?

named

Default Value

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Include <string[]>

Retrieves only the specified items. The value of this parameter qualifies the Path parameter. Enter a path element or pattern, such as "*.txt". Wildcards are permitted.

Required?

false

Position?

named

Default Value

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-LiteralPath <string[]>

Specifies the path to an item. Unlike Path, 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.

Required?

true

Position?

1

Default Value

Accept Pipeline Input?

true (ByPropertyName)

Accept Wildcard Characters?

false

-Path <string[]>

Specifies the path to an item. Get-Content retrieves the content of the item. Wildcards are permitted. The parameter name ("Path" or "FilePath") is optional.

Required?

true

Position?

1

Default Value

Accept Pipeline Input?

true (ByPropertyName)

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.

Required?

false

Position?

named

Default Value

Accept Pipeline Input?

true (ByPropertyName)

Accept Wildcard Characters?

false

-TotalCount <Int64>

Specifies how many lines of content are retrieved. The default is -1 (all lines).

Required?

false

Position?

named

Default Value

Accept Pipeline Input?

true (ByPropertyName)

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

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

<CommonParameters>

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

Inputs and Outputs

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

Inputs

None

You cannot pipe input to Get-Content.

Outputs

Object

Get-Content returns objects that represent the content that it gets. The object type depends on the content type.

Notes

You can also refer to Get-Content by its built-in aliases, "cat", "type" and "gc". For more information, see about_Aliases.

The Get-Content cmdlet is designed to work with the data exposed by any provider. To list the providers available in your session, type "Get-PsProvider". For more information, see about_Providers.

Example 1

C:\PS>get-content -Path C:\Chapters\chapter1.txt

Description
-----------
This command displays the content of the Chapter1.txt file on the console. It uses the Path parameter to specify the name of the item. Get-Content actually passes the content down the pipeline, but because there are no other pipeline elements, the content is formatted and displayed on the console.





Example 2

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

Description
-----------
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 retrieval to the first 50 lines. The pipeline operator (|) sends the result to Set-Content, which places it in the sample.txt file.





Example 3

C:\PS>(get-content cmdlets.txt -totalcount 5)[-1]

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





See Also

Concepts

about_Providers
Add-Content
Set-Content
Clear-Content