powershellscripts.com

Tutorials  PowerShell Cmdlet Help for Get-Content



NAME
Get-Content

SYNOPSIS
Gets the content of the item at the specified location.

SYNTAX
Get-Content [-LiteralPath] [-Credential ] [-Exclude ] [-Filter ] [-Force
] [-Include ] [-ReadCount ] [-TotalCount ] [-UseTransaction] []

Get-Content [-Path] [-Credential ] [-Exclude ] [-Filter ] [-Force] [-Inc
lude ] [-ReadCount ] [-TotalCount ] [-UseTransaction] []


DESCRIPTION
The Get-Content cmdlet gets the content of the item at the location specified by the path, such as the text in a fi
le. It reads the content one line at a time and returns an object for each line.


PARAMETERS
-Credential
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
Omits the specified items. The value of this parameter qualifies the Path parameter. Enter a path element or pa
ttern, such as "*.txt". Wildcards are permitted.

Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false

-Filter
Specifies a filter in the provider's format or language. The value of this parameter qualifies the Path paramet
er. The syntax of the filter, including the use of wildcards, depends on the provider. Filters are more efficie
nt than other parameters, because the provider applies them when retrieving the objects, rather than having Win
dows 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
. For example, Force will override the read-only attribute or create directories to complete a file path, but i
t will not attempt to change file permissions.

Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false

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

Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false

-LiteralPath
Specifies the path to an item. Unlike Path, the value of LiteralPath is used exactly as it is typed. No charact
ers 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
Specifies the path to an item. Get-Content retrieves the content of the item. Wildcards are permitted. The para
meter name ("Path" or "FilePath") is optional.

Required? true
Position? 1
Default value
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? false

-ReadCount
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 conte
nt. As the value of ReadCount increases, the time it takes to return the first line increases, but the total ti
me 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
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


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

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


NOTES


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

The Get-Content cmdlet is designed to work with the data exposed by any provider. To list the providers availab
le 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 th
e name of the item. Get-Content actually passes the content down the pipeline, but because there are no other pipel
ine 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 fi
ve lines and then uses array notation to get the last line (indicated by "-1") of the resulting set.






RELATED LINKS
Online version: http://go.microsoft.com/fwlink/?LinkID=113310
about_Providers
Add-Content
Set-Content
Clear-Content