# Retrieve the content of the primary stream. # Retrieve the content of the primary, or $DATA stream. PSPath : \FileSystem::C:\Test\Stream.txt::$DATA # Specify a wildcard to the Stream parameter to display all streams of the recently created file. \Stream.txt -Value 'This is the content of the Stream.txt file' To create sample content in a file named Stream.txt. In this example, the Set-Content cmdlet is used
Stream for files stored on a Windows NTFS volume.
This example describes how to use the Stream parameter to get the content of an alternate data Example 5: Get the content of an alternate data stream This method isįaster than retrieving all of the lines and using the index notation. The Tail parameter gets the last line of the file. This example uses the Get-Item cmdlet to demonstrate that you can pipe files into the
This example uses the LineNumbers.txt file This command gets the last line of content from a file. Example 4: Get the last line of a text file The last index in the returned array of 25 retrieved lines. Get-Contentreturns an array of lines, this allows you to add the index notationĪfter the parenthesis to retrieve a specific line number. The Get-Content command is wrapped in parentheses so that the command completes before going to LineNumbers.txt file that was created in Example 1. The TotalCount parameter gets the first 25 lines of content. This command gets a specific number of lines from a file and then displays only the last line of This is Line 5 Example 3: Get a specific line of content from a text file This example uses the LineNumbers.txt file that was created inĮxample 1.
The TotalCount parameter is used to gets theįirst five lines of content. This command gets the first five lines of a file. Example 2: Limit the number of lines Get-Content returns Uses the Path parameter to specify the LineNumbers.txt file and displays the content in the $_ represents the array values as each object is sent down the pipeline. Uses a script block with the Add-Content cmdlet to create the LineNumbers.txt file. The array values 1-100 are sent down the pipeline to the ForEach-Object cmdlet. The LineNumbers.txt fileĬontains 100 lines in the format, This is Line X and is used in several examples. This example gets the content of a file in the current directory. Examples Example 1: Get the content of a text file For files, the content is read one line at a timeĪnd returns a collection of objects, each of which represents a line of content.īeginning in PowerShell 3.0, Get-Content can also get a specified number of lines from theīeginning or end of an item. The text in a file or the content of a function. The Get-Content cmdlet gets the content of the item at the location specified by the path, such as Gets the content of the item at the specified location.