Skip to main content This browser is no longer supported. Show
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Get-Content
Gets the content of the item at the specified location. In this articleSyntax
DescriptionThe Beginning in PowerShell 3.0, ExamplesExample 1: Get the content of a text fileThis example gets the content of a file in the current directory. The
The array values 1-100 are sent down the pipeline to the Example 2: Limit the number of lines Get-Content returnsThis command gets the first five lines of a file. The TotalCount parameter is used to gets the first five lines of content. This example uses the
Example 3: Get a specific line of content from a text fileThis
command gets a specific number of lines from a file and then displays only the last line of that content. The TotalCount parameter gets the first 25 lines of content. This example uses the
The Example 4: Get the last line of a text fileThis command gets the last line of content from a file. This example uses the
This example uses the Example 5: Get the content of an alternate data streamThis example describes how to use the Stream parameter to get the content of an alternate data stream for files stored on a Windows NTFS volume. In this example, the
The Stream parameter is a dynamic parameter of the FileSystem provider. By default Example 6: Get raw contentThe commands in this example get the contents of a file as one string, instead of an array of strings. By default, without the Raw dynamic parameter, content is returned as an array of newline-delimited strings. This example uses the
Example 7: Use Filters with Get-ContentYou can specify a filter to the The following command gets the content of all
Example 8: Get file contents as a byte arrayThis example demonstrates how to get the contents of a file as a
The first command uses the AsByteStream parameter to get the stream of bytes from the file. The Raw parameter ensures that the bytes are returned as a Parameters-AsByteStreamSpecifies that the content should be read as a stream of bytes. The AsByteStream parameter was introduced in Windows PowerShell 6.0. A warning occurs when you use the AsByteStream parameter with the Encoding parameter. The AsByteStream parameter ignores any encoding and the output is returned as a stream of bytes.
-CredentialNote This parameter is not supported by any providers installed with PowerShell. To impersonate another user, or elevate your credentials when running this cmdlet, use Invoke-Command.
-DelimiterSpecifies the delimiter that You can use this parameter to split a large file into smaller files by specifying a file separator, as the delimiter. The delimiter is preserved (not discarded) and becomes the last item in each file section. Delimiter is a dynamic parameter that the FileSystem provider adds to the Note Currently, when the value of the Delimiter parameter is an empty string,
-EncodingSpecifies the type of encoding for the target file. The default value is The acceptable values for this parameter are as follows:
Encoding is a dynamic parameter that the FileSystem provider adds to the When reading from and writing to binary files, use the AsByteStream parameter and a value of 0 for the ReadCount parameter. A ReadCount value of 0 reads the entire file in a single read operation. The default ReadCount value, 1, reads one byte in each read operation and converts each byte into a separate object, which causes errors when you use the Beginning with PowerShell 6.2, the Encoding parameter also allows numeric IDs of registered code pages (like Note UTF-7* is no longer recommended to use. As of PowerShell 7.1, a warning is written if you specify
-ExcludeSpecifies, as a string array, an item or items that this cmdlet excludes in the operation. The value of this parameter qualifies the Path parameter. Enter a path element or pattern, such as The Exclude parameter is effective only when the command includes the contents of an item, such as
-FilterSpecifies a filter to qualify the Path parameter. The FileSystem provider is the only installed PowerShell provider that supports the use of filters. You can find the syntax for the FileSystem filter language in about_Wildcards. Filters are more efficient than other parameters, because the provider applies them when the cmdlet gets the objects rather than having PowerShell filter the objects after they are retrieved.
-ForceForce will override a read-only attribute or create directories to complete a file path. The Force parameter does not attempt to change file permissions or override security restrictions.
-IncludeSpecifies, as a string array, an 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
-LiteralPathSpecifies a path to one or more locations. 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 PowerShell not to interpret any characters as escape sequences. For more information, see about_Quoting_Rules.
-PathSpecifies the path to an item where
-RawIgnores newline characters and returns the entire contents of a file in one string with the newlines preserved. By default, newline characters in a file are used as delimiters to separate the input into an array of strings. This parameter was introduced in PowerShell 3.0. Raw is a dynamic parameter that the FileSystem provider adds to the
-ReadCountSpecifies 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 large items.
-StreamNote This Parameter is only available on Windows. Gets the contents of the specified alternate NTFS file stream from the file. Enter the stream name. Wildcards are not supported. Stream is a dynamic parameter that the FileSystem provider adds to the This parameter was introduced in Windows PowerShell 3.0. In PowerShell 7.2, Get-Content can retrieve the content of alternative data streams from directories as well as files.
-TailSpecifies the number of lines from the end of a file or other item. You can use the Tail parameter name or its alias, Last. This parameter was introduced in PowerShell 3.0.
-TotalCountSpecifies 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.
-WaitKeeps the file open after all existing lines have been output. While waiting, Wait is a dynamic parameter that the FileSystem provider adds to the
InputsSystem.Int64, System.String[], System.Management.Automation.PSCredential You can pipe the read count, total count, paths, or credentials to OutputsSystem.Byte, System.String
NotesThe
FeedbackSubmit and view feedback for |