Get-Content | Taking on PowerShell one cmdlet at a time | Weekly Blog

Share this post:

This is a part of an on-going blog series written by Adam Gordon. Each week, Adam will walk you through a PowerShell command, showing you when and how to use each one. This week, Adam covers Get-Content.

When to use Get-Content?

The Get-Content cmdlet gets the content of the item at the location specified by the path, such as the text in a file or the content of a function.

For files, the content is read one line at a time and returns a collection of objects, each of which represents a line of content.

Beginning in PowerShell 3.0, Get-Content can also get a specified number of lines from the beginning or end of an item.

What version of PowerShell am I using for this blog?

Get the PowerShell Version from your machine:


This command shows you the PowerShell version information on your machine.

How to use Get-Content?

Get the content of a text file:

Line # 1:   1..100 | ForEach-Object { Add-Content -Path .\LineNumbers.txt -Value “This is line $_.” }

Line # 2:   Get-Content -Path .\LineNumbers.txt

This example gets the content of a file in the current directory. The LineNumbers.txt file contains 100 lines in the format, “This is Line X”

Line # 1 – The array values 1-100 are sent down the pipeline to the ForEach-Object cmdlet. ForEach-Object uses a script block with the Add-Content cmdlet to create the LineNumbers.txt file. The variable $_ represents the array values as each object is sent down the pipeline.

Line # 2 – The Get-Content cmdlet uses the –Path parameter to specify the LineNumbers.txt file and displays the content in the PowerShell console.

Limit the number of lines Get-Content returns: 

Get-Content -Path .\LineNumbers.txt -TotalCount 5

The –TotalCount parameter is used to get the first five lines of content.

Get a specific line of content from a text file:

 (Get-Content -Path .\LineNumbers.txt -TotalCount 25)[-1]

The –TotalCount parameter gets the first 25 lines of content.

The Get-Content command is wrapped in parentheses so that the command completes before going to the next step.

Get-Content returns an array of lines, this allows you to add the index notation after the parenthesis to retrieve a specific line number. In this case, the [-1] index specifies the last index in the returned array of 25 retrieved lines.

Get the last line of a text file:

 Get-Item -Path .\LineNumbers.txt | Get-Content -Tail 1

This example uses the Get-Item cmdlet to demonstrate that you can pipe files into the Get-Content cmdlet. The –Tail parameter gets the last line of the file. This method is faster than retrieving all of the lines and using the [-1] index notation.

Get the content of an alternate data stream:

Line # 1:    Set-Content -Path .\Stream.txt -Value ‘This is the content of the Stream.txt file’

Line # 2:    Get-Item -Path .\Stream.txt -Stream *

Line # 3:    Get-Content -Path .\Stream.txt -Stream $DATA

Line # 4:    Add-Content -Path .\Stream.txt -Stream NewStream -Value ‘Added a stream named NewStream to Stream.txt’

Line # 5:    Get-Item -Path .\Stream.txt -Stream *

Line # 6:    Get-Content -Path .\Stream.txt -Stream NewStream

Line # 1 the Set-Content cmdlet is used to create sample content in a file named Stream.txt.

Line # 2 specifies a wildcard to the -Stream parameter to display all streams of the recently created file.

Line # 3 retrieves the content of the primary, or $DATA stream.

Line # 4 uses the -Stream parameter of Add-Content to create a new Stream containing sample content.

Line # 5 uses Get-Item to verify the stream was created.

Line # 6 retrieves the content of your newly created Stream.

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

The –Stream parameter is a dynamic parameter of the FileSystem provider. By default, Get-Content only retrieves data from the primary, or $DATA stream. Streams can be used to store hidden data such as attributes, security settings, or other data.

Use Filters with Get-Content:

Get-Content -Path C:\Windows\temp\* -Filter *.log

When using filters to qualify the –Path parameter, you need to include a trailing asterisk (*) to indicate the contents of the path.

Learn last week’s command: Get-Counter.

Need PowerShell training? Check out ITProTV’s PowerShell online IT training courses.