![]() The following command gets the content of all *.log files in the C:\Temp directory. Parameter, you need to include a trailing asterisk ( *) to indicate the contents of the You can specify a filter to the Get-Content cmdlet. Write-Host "Lines contains $($lines.Count) lines." Write-Host "Raw contains $($raw.Count) lines." This example uses the LineNumbers.txt file referenced in Example 1. By default, without the Raw dynamic parameter, content is returned as an array of The commands in this example get the contents of a file as one string, instead of an array of Stored on directories without being child items. Streams canīe used to store hidden data such as attributes, security settings, or other data. The Stream parameter is a dynamic parameter of theīy default Get-Content only retrieves data from the default, or :$DATA stream. \Stream.txt -Stream NewStreamĪdded a stream named NewStream to Stream.txt # Retrieve the content of your newly created Stream. PSPath : \FileSystem::C:\Test\Stream.txt:NewStream Value = 'Added a stream named NewStream to Stream.txt'Īdd-Content Use Get-Item to verify the stream was created. # Use the Stream parameter of Add-Content to create a new Stream containing sample content. # This gets the same data as the prior two examples. # The primary stream doesn't need to be specified to get the primary stream of the file. # Alternative way to get the same content. This is the content of the Stream.txt file # Note the single quotes to prevent variable substitution. # Retrieve the content of the primary 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 Lines in a variable and using the index notation. This method is faster than retrieving all the ![]() Tail parameter gets the last line of the file. This example uses the Get-Item cmdlet to demonstrate that you can pipe files to Get-Content. 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 referenced 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 referenced in Example 1. The TotalCount parameter gets the first five 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. Has 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 representing 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.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |