Introduction to PowerShell Get-Content
Get-Content in the PowerShell is used to read the content from the file (text files) or the program from the specified location. This cmdlet reads the content of the file one at a time and returns as a collection of objects. From PowerShell 3.0 onwards, you can get the specified number of lines from the beginning or the end of the item.
Syntax:
Get-Content
[-ReadCount <Int64>]
[-TotalCount <Int64>]
[-Tail <Int32>]
[-Path] <String[]>
[-LiteralPath] <String[]>
[-Filter <String>]
[-Include <String[]>]
[-Exclude <String[]>]
[-Force]
[-Credential <PSCredential>]
[-Delimiter <String>]
[-Wait]
[-Raw]
[-Encoding <Encoding>]
[-AsByteStream]
[-Stream <String>]
[<CommonParameters>]
Parameters:
- Path: Path of the file to be read. You can provide one or more paths of the files but not the directories. Wildcard characters are permitted.
- Read Count: Specifies the number of lines of the content sent through the pipeline at a time. The Default value is 1 means it sends one line at a time. If you set the value to 0 then it sends the whole content at a time. This parameter doesn’t change the content it displays but it does affect the time to display the content. The more the read count value is, the more time it will take to reach the first line but overall operation time decreases.
- TotalCount: Specifies the total number of lines to display from the beginning. If you set the value 10, it will display 10 lines only. The default value is -1, which means all the lines will be displayed. Its aliases are Head and First.
- Tail: Specifies the total number of lines to display from the end of the file. Its alias is Last.
- Include: Include parameter is path qualifier means it includes all the items from that path. Wildcard character (*) is permitted. When you use –Include parameter then you need to provide content of the item. For example, D:\temp\* where (*) specifies the contents of the directory.
- Exclude: Exclude parameter is path qualifier means it excludes all the items which are specified from that path. Wildcard character (*) is permitted. When you use –Exclude parameter then you need to provide content of the item. For example, D:\Temp\* where (*) specifies the contents of the directory.
- Filter: The filter parameter is more efficient than include or exclude. It is also a path qualifier and you can use the wildcard character (*) for this parameter.
- Force: Retrieves the contents from the file which is restricted by any settings except security permissions. Force parameter overrides the read-only attribute of the file or creates a directory to complete a file path.
- Credential: When the file is located on a different domain or in Workgroup, you can use the credentials of the file of that location to retrieve its contents.
- Delimiter: While retrieving the contents from the file, it uses a delimiter character to split the files into string objects. The default delimiter is (\n), end of the line character.
- Wait: When the wait parameter is specified, the Powershell console keeps the file open and waits until manually interrupted by entering CTRL + C or by deleting the file. In the second case, there will be a non-terminating error. The wait cannot be combined with Raw Parameter.
- Raw: Returns the multiple lines as a single string but preserves the new lines in the output.
- Encoding: Specifies the type of encoding for the target file. Accepted encoding values.
- AsByteStream: This parameter was introduced in PowerShell 6.0 and specifies if the contents should be read as a byte of the stream.
- Stream: This parameter is used to create the stream. Once you create a different stream, you can retrieve changes in the files according to the stream.
- LiteralPath: This parameter specifies the path of one or more locations. Unlike -path parameter, you cannot specify wildcard characters here because this parameter can’t interpret characters as wildcards. If your path includes any escape characters then mark them under a single quote and PowerShell will consider it as a single path.
- CommonParameters: Below common parameters are used which are also called advance function’s parameters.
Verbose,Debug, ErrorAction, ErrorVariable, WarningAction, WarningAction, WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
Examples of PowerShell Get-Content
The examples of the PowerShell Get-Content is given below:
Example #1
Read file through Get-Content.
In the testreadC.txt file, we have stored processes.
Get-Content -Path D:\Temp\testreadC.txt
Output:
Example #2
GC with TotalCount
Get-Content -Path D:\Temp\testreadC.txt -TotalCount 10
A total of 10 lines will be displayed from the beginning.
Output:
Example #3
GC with Tail
Get-Content -Path D:\Temp\testreadC.txt -Tail 10
A total of 10 lines will be displayed from the bottom.
Output:
Example #4
GC with ReadCount
Get-Content -Path D:\Temp\testreadC.txt -ReadCount 10 | Set-Content D:\Temp\ReadC.txt
The above example will send 10 lines at a time to new file ReadC.txt.
Example #5
GC with Delimiter
Get-Content D:\Temp\delim.txt -Delimiter '@'
The above command will split the file content with ‘@’ character and start a new line after it.
For example,
Example #6
GC with Raw
Get-Content D:\Temp\testreadC.txt -Raw
The above command will store the entire file content into a single string instead of an array. We can check the line count as below.
Output:
Example # 7
GC with Encoding
Get-Content D:\Temp\testreadC.txt -Encoding Byte -TotalCount 10
Here, encoding Byte is selected so the data will be in byte format. You can select another formatting as mentioned above in parameter explanation.
Output:
Example #8
GC with Stream
Get-Content D:\Temp\stream1.txt -Stream $stream1
The above command will keep the existing content in the stream1. When new content is added with the new stream in the same file then it can be recognized easily.
Set-Content D:\Temp\stream1.txt -Value "This is the new line" -
Stream $stream2
Get-Content D:\Temp\stream1.txt -Stream $stream2
Output:
Example #9
GC with Filter / Include / Exclude parameter.
Get-Content D:\Temp\* -Filter Testread*
The above command will read all the files that start with “TestRead” in the file name.
Get-Content D:\Temp\* -Include *read*
The above command will read all the files that contain “Read” in the file name.
Get-Content D:\Temp\* -Exclude Test*
The above command will exclude all the files that start with “Test” in the file name and will read content from other files on the same path.
Recommended Articles
This is a guide to PowerShell Get-Content. Here we discuss the introduction, Parameters for the given Syntax and the Examples of PowerShell Get-Content. You can also go through our other related articles to learn more–
- PowerShell Set-Location
- VI Editor in Unix
- cmdlets in PowerShell
- PowerShell Set-Content
- PowerShell Break | Top 8 Examples
- PowerShell Convert to String | Parameters
360+ Online Courses | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7
View Course
Related Courses