Share this post: This is part of an ongoing blog series by Adam Gordon. Adam will show you how to use each PowerShell command each week. Adam will be covering Get-Content this week.
When should you use Get-Content
The Get-Content cmdlet retrieves the content at the specified location, such as text in a file or content of a function.
Files are read one line at time. This returns a list of objects that each represent a line of content.
PowerShell 3.0’s Get-Content allows you to get a specific number of lines starting at the beginning or ending of an item.
What version of PowerShell should I use for this blog?
Get the PowerShell Version for your machine
This command displays the PowerShell version information for your machine.
How to use Get Content?
Get the text content of a file:
ForEach-Object Add-Content -Path .\LineNumbers.txt -Value “This is line $_.”
Line # 2: Get Content – Path.LineNumbers.txt
This example shows the contents of a file in the current director. LineNumbers.txt contains 100 lines according to the format “This is Line X”.
Line # 1 – The array values 1-100 are sent along the pipeline to ForEach-Object cmdlet. To create the LineNumbers.txt files, ForEach-Object uses a scriptblock with the Add-Content cmdlet. As each object is sent down the pipeline, the variable $_ records the array values.
Line #2 – The Get Content cmdlet uses a -Path parameter for the LineNumbers.txt text file. It then displays the content in PowerShell’s console.
Limit the number lines that can be returned with Get-Content returns
Get-Content -Path .\LineNumbers.txt -TotalCount 5
The -TotalCount parameter can be used to retrieve the first five lines.
You can get a specific line from a text file.
(Get-Content -Path .\LineNumbers.txt -TotalCount 25)[-1]
The first 25 lines of content are automatically included in the -TotalCount parameter.
The Get-Content command must be enclosed in parentheses to ensure that it completes before moving on to the next step.
Get-Content returns an array with lines. This allows you to add the index note after the parenthesis to retrieve a specific number. In this example, the [-1] index indicates the last index in the returned array with 25 lines.
Find the last line in a text file
Get-Item -Path .\LineNumbers.txt | Get-Content -Tail 1
This example shows that you can pipe files into Get-Content cmdlet. The -Tail parameter retrieves the last line of the file. This method is faster than using the [-1] index notation to retrieve all lines.
Get the content of an alternative data stream:
Line #1: Set-Content-Path.Stream.txt-Value “This is the content for the Stream.txt files’
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 into Stream.txt.”
Line # 5: Get -Item.Stream.txt-Stream *
Line # 6: Get Content -Path.Stream.txt-Stream NewStream
Line #1: The Set-Content cmdlet can be used to create sample content within a file called Stream.txt.
Line #2 specifies a wildcard for the -Stream parameter that will display all streams in the newly created file.
Line 3 retrieves $DATA stream content.
Line #4 uses the -Stream parameter in Add-Content for creating a new stream containing sample content.
Line #5 uses Get-Item for verification that the stream was created.
Line 6 retrieves the content from your newly created Stream.
This example shows how to use -Stream to retrieve alternate data streams for files stored on a Windows NTFS volume.
The dynamic parameter -Stream is part of the FileSystem provider. Get-Content retrieves data only from the $DATA stream or primary stream by default. Hidden data like attributes, security settings, and other data can be stored in streams.
Use filters with Get-Content
Get-Content -Path C:\Windows\temp\* -Filter *.log
To indicate the path’s contents, you must include a trailing asterisk (“*”) when using filters to qualify -Path.
Learn the command for last week: Get-Counter
Do you need PowerShell training? ITProTV offers PowerShell online IT training courses.