Get-Content

 

Additional Resources for Get-Content

 

Reading a Text File

http://www.microsoft.com/technet/scriptcenter/topics/msh/cmdlets/get-content.mspx

 

 

SYNOPSIS

Gets the content of the item at the specified location.

 

SYNTAX

Get-Content [-path] <string[]> [-totalCount <long>] [-readCount <long>] [-include <string[]>] [-exclude <string[]>] [-filter <string>] [-force] [-credential <PSCredential>] [-encoding {<Unknown> | <String> | <Unicode> | <Byte> | <BigEndianUnicode> | <UTF8> | <UTF7> | <Ascii>}] [<CommonParameters>]

 

Get-Content [-literalPath] <string[]> [-totalCount <long>] [-readCount <long>] [-include <string[]>] [-exclude <string[]>] [-filter <string>] [-force] [-credential <PSCredential>] [-encoding {<Unknown> | <String> | <Unicode> | <Byte> | <BigEndianUnicode> | <UTF8> | <UTF7> | <Ascii>}] [<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 an object for each line.

 

PARAMETERS

 

-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

N/A - The path must be specified

Accept pipeline input?  

true (ByPropertyName)

Accept wildcard characters? 

true

 

-totalCount <long>

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

 

Required?

false

Position?

named

Default value

0

Accept pipeline input?  

true (ByPropertyName)

Accept wildcard characters? 

false

 

-readCount <long>

Specifies how many lines of content are sent through the pipeline at a time. The default value, 0, 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

1

Accept pipeline input?  

true (ByPropertyName)

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? 

true

 

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

true

 

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

Overrides restrictions that prevent the command from succeeding, just so the changes do not compromise security. For example, Force will override the read-only attribute or create directories to complete a file path, but it will not attempt to change file permissions.

 

Required?

false

Position?

named

Default value

False

Accept pipeline input?   

false

Accept wildcard characters? 

false

 

-credential <PSCredential>

Uses a credential to validate access to the file. <Credential> represents a user-name, such as "User01" or "Domain01\User01", or a PSCredential object, such as the one retrieved by using the Get-Credential cmdlet. If you type a user name, you will be prompted for a password. This parameter appears, but it is not supported in any Windows PowerShell core cmdlets or providers.

 

Required?

false

Position?

named

Default value

 

Accept pipeline input?  

true (ByPropertyName)

Accept wildcard characters? 

false

 

-encoding <FileSystemCmdletProviderEncoding>

Specifies the type of character encoding used to display the content. This parameter works only with the Windows PowerShell FileSystem provider.

 

The following lists the acceptable values for this parameter:

 

·          Unknown

·          String

·          Unicode

·          Byte

·          BigEndian

·          Unicode

·          UTF8

·          UTF7

·          Ascii

 

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

 

<CommonParameters>

This cmdlet supports the common parameters: -Verbose, -Debug, -ErrorAction, -ErrorVariable, and -OutVariable. For more information, type, "get-help about_commonparameters".

 

NOTES

 

For more information, type "Get-Help Get-Content -detailed". For technical information, type "Get-Help Get-Content -full".

 

 When specifying multiple values for a parameter, use commas to separate the values. For example, "<parameter-name> <value1>, <value2>".

 

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

 

EXAMPLE 1

 

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

 

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

 

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

 

This command gets the first 50 lines of the Log060912.txt file and stores it 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-Contentwhich places it in the sample.txt file.

 

RELATED LINKS

Add-Content

Set-Content

Clear-Content

about_namespace