{"id":7724,"date":"2023-02-07T13:59:00","date_gmt":"2023-02-07T11:59:00","guid":{"rendered":"https:\/\/lazyadmin.nl\/?p=7724"},"modified":"2024-01-11T15:34:23","modified_gmt":"2024-01-11T13:34:23","slug":"get-content","status":"publish","type":"post","link":"https:\/\/lazyadmin.nl\/powershell\/get-content\/","title":{"rendered":"How to use PowerShell Get-Content to Read a File"},"content":{"rendered":"\n<p>The Get-Content cmdlet can be used to retrieve the contents from a file in PowerShell. It will retrieve the contents one line at a time, storing them in an array. This allows you to use or process each line in your PowerShell script.<\/p>\n\n\n\n<p><strong>Get-Content <\/strong>comes with several parameters that allow you to select or filter the results you want to retrieve from the file. And when you need to process a lot of large files, then using the <code>-raw<\/code> parameter can speed up the process.<\/p>\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">In this article<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-69f228a495d39\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"ez-toc-cssicon\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\" style='display:none' id=\"ez-toc-cssicon-toggle-item-69f228a495d39\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/lazyadmin.nl\/powershell\/get-content\/#read-file-with-get-content\" >Read File with Get-Content<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/lazyadmin.nl\/powershell\/get-content\/#returning-the-first-or-last-x-lines\" >Returning the First or Last x Lines<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/lazyadmin.nl\/powershell\/get-content\/#return-a-single-line-from-a-file\" >Return a single line from a file<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/lazyadmin.nl\/powershell\/get-content\/#reading-multiple-files-with-get-content\" >Reading Multiple Files with Get-Content<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/lazyadmin.nl\/powershell\/get-content\/#using-raw\" >Using -Raw<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/lazyadmin.nl\/powershell\/get-content\/#reading-files-faster-with-readalllines\" >Reading files faster with ReadAllLines<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/lazyadmin.nl\/powershell\/get-content\/#watch-files-with-wait\" >Watch files with -Wait<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/lazyadmin.nl\/powershell\/get-content\/#wrapping-up\" >Wrapping Up<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n\n<p>In this article, we are going to take a look at how to use the Get-Content cmdlet, and explain the different parameters that we can use.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"read-file-with-get-content\"><\/span>Read File with Get-Content<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>We are going to start with the basics, read a file in PowerShell line-by-line using the <code>Get-Content<\/code> cmdlet. For the examples below I will be using a text file with 100 lines, which are numbered. This makes it easier to follow the examples:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">1..100 | ForEach-Object { Add-Content -Path .\\LineNumbers.txt -Value \"This is line $_.\" }<\/pre>\n\n\n\n<p>The code above is from <a href=\"https:\/\/learn.microsoft.com\/en-us\/powershell\/module\/microsoft.powershell.management\/get-content?view=powershell-7.3\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Microsoft<\/a> and creates a text file where each line starts with the  string &#8220;This is line &#8221; followed by the line number:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"879\" height=\"266\" src=\"https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/2023-02-03_12h16_22.jpg\" alt=\"PowerShell Get-Content\" class=\"wp-image-7725\" srcset=\"https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/2023-02-03_12h16_22.jpg 879w, https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/2023-02-03_12h16_22-300x91.jpg 300w, https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/2023-02-03_12h16_22-768x232.jpg 768w, https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/2023-02-03_12h16_22-800x242.jpg 800w, https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/2023-02-03_12h16_22-400x121.jpg 400w, https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/2023-02-03_12h16_22-50x15.jpg 50w\" sizes=\"(max-width: 879px) 100vw, 879px\" \/><\/figure>\n\n\n\n<p>To view the results of the newly created file, we can use the <code>Get-Content<\/code> cmdlet. You only need to specify the path to the file to start reading its contents of it:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Get-Content -Path .\\LineNumbers.txt<\/pre>\n\n\n\n<p>If we store the result of Get-Content in a variable, then we can see that PowerShell stores the results in an array. This means that we can use any <a href=\"https:\/\/lazyadmin.nl\/powershell\/powershell-array\/\">Array function<\/a> on the results, or process each line using a <a href=\"https:\/\/lazyadmin.nl\/powershell\/powershell-loops-for-foreach-do-while-until\/\">ForEach loop<\/a>:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$contents = Get-Content -Path .\\LineNumbers.txt\n$contents.GetType()\n\n# Result \nIsPublic IsSerial Name                                     BaseType\n-------- -------- ----                                     --------\nTrue     True     Object[]                                 System.Array\n\n# Return the 6th line (the index of an array start at 0 ;) )\n$contents[5]\n\n# Result\nThis is line 6<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"879\" height=\"371\" src=\"https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/2023-02-03_12h24_42.jpg\" alt=\"Read file contents with PowerShell\" class=\"wp-image-7726\" srcset=\"https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/2023-02-03_12h24_42.jpg 879w, https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/2023-02-03_12h24_42-300x127.jpg 300w, https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/2023-02-03_12h24_42-768x324.jpg 768w, https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/2023-02-03_12h24_42-800x338.jpg 800w, https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/2023-02-03_12h24_42-400x169.jpg 400w, https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/2023-02-03_12h24_42-50x21.jpg 50w\" sizes=\"(max-width: 879px) 100vw, 879px\" \/><figcaption class=\"wp-element-caption\">Using a ForEach loop on the Get-Contents result<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"returning-the-first-or-last-x-lines\"><\/span>Returning the First or Last x Lines<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>By default, the Get-Content cmdlet will return all the contents of a file in PowerShell. But in some cases, you only want to view the first or last x lines from a file. For example, when debugging an issue, you often only need the last couple of lines from a log file.<\/p>\n\n\n\n<p>For this we have two options, we can use the parameter <code>-TotalCount<\/code>, which returns the first x items of the content. Or we can use the parameter <code>-Tail<\/code>, which returns the last x items of a file.<\/p>\n\n\n\n<p>So let&#8217;s go back to our log file example. We want to view the last 10 lines from the log. To do this, we need to specify the path and add <code>-Tail<\/code> 10 to return only the last 10 lines:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Get-Content C:\\temp\\files\\la-srv-dc01.log -Tail 10<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"873\" height=\"356\" src=\"https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/2023-02-07_10h37_01.jpg\" alt=\"PowerShell Get-Content return last x lines\" class=\"wp-image-7728\" srcset=\"https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/2023-02-07_10h37_01.jpg 873w, https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/2023-02-07_10h37_01-300x122.jpg 300w, https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/2023-02-07_10h37_01-768x313.jpg 768w, https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/2023-02-07_10h37_01-800x326.jpg 800w, https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/2023-02-07_10h37_01-400x163.jpg 400w, https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/2023-02-07_10h37_01-50x20.jpg 50w\" sizes=\"(max-width: 873px) 100vw, 873px\" \/><figcaption class=\"wp-element-caption\">Get-Content return the last x lines<\/figcaption><\/figure>\n\n\n\n<p>To read the first x item from a file we will need to use the parameter <code>-TotalCount<\/code>:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Get-Content -Path .\\LineNumbers.txt -TotalCount 5<\/pre>\n\n\n<script async src=\"https:\/\/alatus.eocampaign1.com\/form\/4217287c-b05c-11ee-a0ac-e5852477572a.js\" data-form=\"4217287c-b05c-11ee-a0ac-e5852477572a\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"return-a-single-line-from-a-file\"><\/span>Return a single line from a file<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>If you know which specific line you need from a text file, then it&#8217;s possible to select only a single line from the text file. As we know, the results are stored in an array. So we can use the array index to select a line number that we need.<\/p>\n\n\n\n<p>Keep in mind that the array index starts at 0, so if we want to return line 16, we need to specify index number 15. We wrap the <code>Get-Content<\/code> command in parentheses <code>( )<\/code> so that PowerShell first completes the Get-Content command before it continues.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">(Get-Content -Path .\\LineNumbers.txt)[15]\n\n# Result\nThis is line 16.<\/pre>\n\n\n\n<p>If you have a large text file then you might not want to read it completely to only return a single line. What you can do is select only the first or last x lines from the file, and then select the line number using the array index.<\/p>\n\n\n\n<p>For example, let&#8217;s say we need the fifth line from a file. We set the <code>TotalCount <\/code>to 5, reading only the first 5 lines, and return the last item from the array:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">(Get-Content -Path .\\LineNumbers.txt -TotalCount 5)[-1]\n\n# Result\nThis is line 5.<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"reading-multiple-files-with-get-content\"><\/span>Reading Multiple Files with Get-Content<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>It&#8217;s also possible to read the contents of multiple files at once with Get-Content in PowerShell. To do this you only need to specify the path where the files are located followed by an asterisk <code>*<\/code>. <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Get-Content -Path c:\\temp\\files\\*<\/pre>\n\n\n\n<p>Keep in mind though that there won&#8217;t be any reference in the results to the source files. So you won&#8217;t know from which file line x comes.<\/p>\n\n\n\n<p>We can also filter which files we want to read. So for example, if you only want to get the contents of all the .log files then you can add the parameter <code>-Filter<\/code> to it:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Get-Content -Path c:\\temp\\files\\* -Filter '*.log'<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"using-raw\"><\/span>Using -Raw<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The Get-Content cmdlet reads each line of the file into the array, which is great when you need to process each line independently or you are looking for a single line. But array a terrible if you need to do a search and replace on the contents of a file.<\/p>\n\n\n\n<p>For example, when you have an HTML file that you are using as a template for an email that you are about the send with PowerShell. In the HTML file, we have placeholders that we need to replace, like this:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;p>Hi {{manager.firstname}},&lt;\/p>\n&lt;p>\n The account for {{user.fullname}} is ready. Below you will find the login details for {{user.firstname}}. We have set a temporary password which must be\n changed after the first login.\n&lt;\/p>\n&lt;p>\n In the attachment you will find a starting guide for new users\n&lt;\/p><\/pre>\n\n\n\n<p>Instead of looping through each line to find the placeholders and replace them, we can use the <code>-Raw<\/code> parameter to read the whole file as a single string and use the <a href=\"https:\/\/lazyadmin.nl\/powershell\/powershell-replace\/\">-Replace operator<\/a> to replace the placeholders:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">(Get-Content -Path c:\\temp\\template.html -Raw) -replace '{{user.fullname}}', $user.fullName -replace '{{user.firstname}}', $user.givenName<\/pre>\n\n\n\n<p>Another advantage of using the -Raw parameter is that it&#8217;s a lot faster. Get-Content works great with small text files, but when you need to process large text files then you will probably notice that the command is a bit sluggish. <\/p>\n\n\n\n<p>For example, a text file of 50Mb takes almost 5 seconds to read as you can see in the screenshot below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"873\" height=\"341\" src=\"https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/2023-02-07_11h40_03.jpg\" alt=\"Read large file with PowerShell\" class=\"wp-image-7730\" srcset=\"https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/2023-02-07_11h40_03.jpg 873w, https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/2023-02-07_11h40_03-300x117.jpg 300w, https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/2023-02-07_11h40_03-768x300.jpg 768w, https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/2023-02-07_11h40_03-800x312.jpg 800w, https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/2023-02-07_11h40_03-400x156.jpg 400w, https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/2023-02-07_11h40_03-50x20.jpg 50w\" sizes=\"(max-width: 873px) 100vw, 873px\" \/><\/figure>\n\n\n\n<p>But when using -Raw it took less than half a second:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"873\" height=\"341\" src=\"https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/image.png\" alt=\"\" class=\"wp-image-7731\" srcset=\"https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/image.png 873w, https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/image-300x117.png 300w, https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/image-768x300.png 768w, https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/image-800x312.png 800w, https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/image-400x156.png 400w, https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/image-50x20.png 50w\" sizes=\"(max-width: 873px) 100vw, 873px\" \/><\/figure>\n\n\n\n<p>If you still need the results in an array then we can use <code>-Raw<\/code> combined with the <code>-split<\/code> string operator to create an array of each line of the file. This method is still 5 times faster than using Get-Content without <code>-Raw<\/code>.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$log = (Get-Content .\\largefile.log -Raw) -split \"`r`n\"<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"reading-files-faster-with-readalllines\"><\/span>Reading files faster with ReadAllLines<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>When processing very large files, 100Mb and large, then there is an even faster method to read the contents of the file with PowerShell. The .Net method <code>System.IO.File.ReadAllLines()<\/code>. will read all the lines into the memory in half of the time compared to using the -Raw method.<\/p>\n\n\n\n<p>The results are, just like with Get-Content, stored in an array. So you can process that data in the same way.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$log = [System.IO.File]::ReadAllLines('c:\\temp\\files\\largefile.log')<\/pre>\n\n\n\n<p>You can also use the method <code>[System.IO.File]::ReadLines<\/code>, which is even faster. But this will immediately stream the contents to the console, instead of storing it in the memory. The problem with this method is that you will need to close the file when done, otherwise, it will be locked by the stream.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"watch-files-with-wait\"><\/span>Watch files with -Wait<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The last interesting function of Get-Contents is that we can monitor or watch the contents of a file using the <code>-Wait<\/code> parameter. If you, for example, want to monitor a log file. Then instead of rerunning the Get-Content cmdlet every time, you can add the <code>-wait<\/code> parameter.<\/p>\n\n\n\n<p>As soon as the contents of the file are updated (and saved), it will update the output of Get-Content.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Get-Content -Path c:\\temp\\testfiles\\LineNumbers.txt -Tail 5 -Wait\n\n# Result\nThis is line 96.\nThis is line 97.\nThis is line 98.\nThis is line 99.\nThis is line 100.<\/pre>\n\n\n\n<p>If we add new lines to the end of the file, then these will show up in the results.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"wrapping-up\"><\/span>Wrapping Up<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The Get-Content cmdlet in PowerShell is great when you need to read a file quickly. But when working with large files you really need to use the -Raw parameter at least to speed up the process. Also, give the .Net method a try, you will be amazed at how fast it is.<\/p>\n\n\n\n<p>I hope you found this article useful, if you have any questions, then just drop a comment below.<\/p>\n\n\n\n<p> <\/p>\n<script async src=\"https:\/\/alatus.eocampaign1.com\/form\/d0ce5de8-9b48-11ee-8cf4-eb425eea7369.js\" data-form=\"d0ce5de8-9b48-11ee-8cf4-eb425eea7369\"><\/script>","protected":false},"excerpt":{"rendered":"<p>The Get-Content cmdlet can be used to retrieve the contents from a file in PowerShell. It will retrieve the contents one line at a time, storing them in an array. This allows you to use or process each line in &#8230; <a title=\"How to use PowerShell Get-Content to Read a File\" class=\"read-more\" href=\"https:\/\/lazyadmin.nl\/powershell\/get-content\/\">Read more<span class=\"screen-reader-text\">How to use PowerShell Get-Content to Read a File<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":7733,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_uag_custom_page_level_css":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[69],"tags":[22],"class_list":["post-7724","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-powershell"],"wppr_data":{"cwp_meta_box_check":"No"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to use PowerShell Get-Content to Read a File &#8212; LazyAdmin<\/title>\n<meta name=\"description\" content=\"Learn how to read file contents quickly in PowerShell with Get-Content and the .Net method ReadAllLines. And how to read multiple files at once\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/lazyadmin.nl\/powershell\/get-content\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to use PowerShell Get-Content to Read a File &#8212; LazyAdmin\" \/>\n<meta property=\"og:description\" content=\"Learn how to read file contents quickly in PowerShell with Get-Content and the .Net method ReadAllLines. And how to read multiple files at once\" \/>\n<meta property=\"og:url\" content=\"https:\/\/lazyadmin.nl\/powershell\/get-content\/\" \/>\n<meta property=\"og:site_name\" content=\"LazyAdmin\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/lazyadminnl\/\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/lazyadminnl\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-02-07T11:59:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-11T13:34:23+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/PowerShell-Get-Contents.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"800\" \/>\n\t<meta property=\"og:image:height\" content=\"450\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Rudy Mens\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/lazyadmin\" \/>\n<meta name=\"twitter:site\" content=\"@lazyadmin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rudy Mens\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/lazyadmin.nl\/powershell\/get-content\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/lazyadmin.nl\/powershell\/get-content\/\"},\"author\":{\"name\":\"Rudy Mens\",\"@id\":\"https:\/\/lazyadmin.nl\/#\/schema\/person\/cbfba61543b21fbba63cfbf62f08d952\"},\"headline\":\"How to use PowerShell Get-Content to Read a File\",\"datePublished\":\"2023-02-07T11:59:00+00:00\",\"dateModified\":\"2024-01-11T13:34:23+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/lazyadmin.nl\/powershell\/get-content\/\"},\"wordCount\":1233,\"commentCount\":5,\"publisher\":{\"@id\":\"https:\/\/lazyadmin.nl\/#\/schema\/person\/cbfba61543b21fbba63cfbf62f08d952\"},\"image\":{\"@id\":\"https:\/\/lazyadmin.nl\/powershell\/get-content\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/PowerShell-Get-Contents.jpg\",\"keywords\":[\"Powershell\"],\"articleSection\":[\"PowerShell\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/lazyadmin.nl\/powershell\/get-content\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/lazyadmin.nl\/powershell\/get-content\/\",\"url\":\"https:\/\/lazyadmin.nl\/powershell\/get-content\/\",\"name\":\"How to use PowerShell Get-Content to Read a File &#8212; LazyAdmin\",\"isPartOf\":{\"@id\":\"https:\/\/lazyadmin.nl\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/lazyadmin.nl\/powershell\/get-content\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/lazyadmin.nl\/powershell\/get-content\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/PowerShell-Get-Contents.jpg\",\"datePublished\":\"2023-02-07T11:59:00+00:00\",\"dateModified\":\"2024-01-11T13:34:23+00:00\",\"description\":\"Learn how to read file contents quickly in PowerShell with Get-Content and the .Net method ReadAllLines. And how to read multiple files at once\",\"breadcrumb\":{\"@id\":\"https:\/\/lazyadmin.nl\/powershell\/get-content\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/lazyadmin.nl\/powershell\/get-content\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/lazyadmin.nl\/powershell\/get-content\/#primaryimage\",\"url\":\"https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/PowerShell-Get-Contents.jpg\",\"contentUrl\":\"https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/PowerShell-Get-Contents.jpg\",\"width\":800,\"height\":450},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/lazyadmin.nl\/powershell\/get-content\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/lazyadmin.nl\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to use PowerShell Get-Content to Read a File\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/lazyadmin.nl\/#website\",\"url\":\"https:\/\/lazyadmin.nl\/\",\"name\":\"LazyAdmin\",\"description\":\"Tips and howto&#039;s about Office 365, PowerShell, Home network and smart devices\",\"publisher\":{\"@id\":\"https:\/\/lazyadmin.nl\/#\/schema\/person\/cbfba61543b21fbba63cfbf62f08d952\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/lazyadmin.nl\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/lazyadmin.nl\/#\/schema\/person\/cbfba61543b21fbba63cfbf62f08d952\",\"name\":\"Rudy Mens\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/lazyadmin.nl\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/lazyadmin.nl\/wp-content\/uploads\/2024\/06\/Profile-photo-2024-Ruud.jpg\",\"contentUrl\":\"https:\/\/lazyadmin.nl\/wp-content\/uploads\/2024\/06\/Profile-photo-2024-Ruud.jpg\",\"width\":500,\"height\":500,\"caption\":\"Rudy Mens\"},\"logo\":{\"@id\":\"https:\/\/lazyadmin.nl\/#\/schema\/person\/image\/\"},\"description\":\"Ruud worked for more than 15 years as a SysAdmin in the Netherlands and is now working as an independent consultant. In his spare time, he loves to thinker with Smart Devices.\",\"sameAs\":[\"https:\/\/lazyadmin.nl\",\"https:\/\/www.facebook.com\/lazyadminnl\/\",\"http:\/\/nl.linkedin.com\/in\/rudymens\/\",\"https:\/\/x.com\/https:\/\/twitter.com\/lazyadmin\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to use PowerShell Get-Content to Read a File &#8212; LazyAdmin","description":"Learn how to read file contents quickly in PowerShell with Get-Content and the .Net method ReadAllLines. And how to read multiple files at once","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/lazyadmin.nl\/powershell\/get-content\/","og_locale":"en_US","og_type":"article","og_title":"How to use PowerShell Get-Content to Read a File &#8212; LazyAdmin","og_description":"Learn how to read file contents quickly in PowerShell with Get-Content and the .Net method ReadAllLines. And how to read multiple files at once","og_url":"https:\/\/lazyadmin.nl\/powershell\/get-content\/","og_site_name":"LazyAdmin","article_publisher":"https:\/\/www.facebook.com\/lazyadminnl\/","article_author":"https:\/\/www.facebook.com\/lazyadminnl\/","article_published_time":"2023-02-07T11:59:00+00:00","article_modified_time":"2024-01-11T13:34:23+00:00","og_image":[{"width":800,"height":450,"url":"https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/PowerShell-Get-Contents.jpg","type":"image\/jpeg"}],"author":"Rudy Mens","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/lazyadmin","twitter_site":"@lazyadmin","twitter_misc":{"Written by":"Rudy Mens","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/lazyadmin.nl\/powershell\/get-content\/#article","isPartOf":{"@id":"https:\/\/lazyadmin.nl\/powershell\/get-content\/"},"author":{"name":"Rudy Mens","@id":"https:\/\/lazyadmin.nl\/#\/schema\/person\/cbfba61543b21fbba63cfbf62f08d952"},"headline":"How to use PowerShell Get-Content to Read a File","datePublished":"2023-02-07T11:59:00+00:00","dateModified":"2024-01-11T13:34:23+00:00","mainEntityOfPage":{"@id":"https:\/\/lazyadmin.nl\/powershell\/get-content\/"},"wordCount":1233,"commentCount":5,"publisher":{"@id":"https:\/\/lazyadmin.nl\/#\/schema\/person\/cbfba61543b21fbba63cfbf62f08d952"},"image":{"@id":"https:\/\/lazyadmin.nl\/powershell\/get-content\/#primaryimage"},"thumbnailUrl":"https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/PowerShell-Get-Contents.jpg","keywords":["Powershell"],"articleSection":["PowerShell"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/lazyadmin.nl\/powershell\/get-content\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/lazyadmin.nl\/powershell\/get-content\/","url":"https:\/\/lazyadmin.nl\/powershell\/get-content\/","name":"How to use PowerShell Get-Content to Read a File &#8212; LazyAdmin","isPartOf":{"@id":"https:\/\/lazyadmin.nl\/#website"},"primaryImageOfPage":{"@id":"https:\/\/lazyadmin.nl\/powershell\/get-content\/#primaryimage"},"image":{"@id":"https:\/\/lazyadmin.nl\/powershell\/get-content\/#primaryimage"},"thumbnailUrl":"https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/PowerShell-Get-Contents.jpg","datePublished":"2023-02-07T11:59:00+00:00","dateModified":"2024-01-11T13:34:23+00:00","description":"Learn how to read file contents quickly in PowerShell with Get-Content and the .Net method ReadAllLines. And how to read multiple files at once","breadcrumb":{"@id":"https:\/\/lazyadmin.nl\/powershell\/get-content\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/lazyadmin.nl\/powershell\/get-content\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/lazyadmin.nl\/powershell\/get-content\/#primaryimage","url":"https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/PowerShell-Get-Contents.jpg","contentUrl":"https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/PowerShell-Get-Contents.jpg","width":800,"height":450},{"@type":"BreadcrumbList","@id":"https:\/\/lazyadmin.nl\/powershell\/get-content\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/lazyadmin.nl\/"},{"@type":"ListItem","position":2,"name":"How to use PowerShell Get-Content to Read a File"}]},{"@type":"WebSite","@id":"https:\/\/lazyadmin.nl\/#website","url":"https:\/\/lazyadmin.nl\/","name":"LazyAdmin","description":"Tips and howto&#039;s about Office 365, PowerShell, Home network and smart devices","publisher":{"@id":"https:\/\/lazyadmin.nl\/#\/schema\/person\/cbfba61543b21fbba63cfbf62f08d952"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/lazyadmin.nl\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/lazyadmin.nl\/#\/schema\/person\/cbfba61543b21fbba63cfbf62f08d952","name":"Rudy Mens","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/lazyadmin.nl\/#\/schema\/person\/image\/","url":"https:\/\/lazyadmin.nl\/wp-content\/uploads\/2024\/06\/Profile-photo-2024-Ruud.jpg","contentUrl":"https:\/\/lazyadmin.nl\/wp-content\/uploads\/2024\/06\/Profile-photo-2024-Ruud.jpg","width":500,"height":500,"caption":"Rudy Mens"},"logo":{"@id":"https:\/\/lazyadmin.nl\/#\/schema\/person\/image\/"},"description":"Ruud worked for more than 15 years as a SysAdmin in the Netherlands and is now working as an independent consultant. In his spare time, he loves to thinker with Smart Devices.","sameAs":["https:\/\/lazyadmin.nl","https:\/\/www.facebook.com\/lazyadminnl\/","http:\/\/nl.linkedin.com\/in\/rudymens\/","https:\/\/x.com\/https:\/\/twitter.com\/lazyadmin"]}]}},"uagb_featured_image_src":{"full":["https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/PowerShell-Get-Contents.jpg",800,450,false],"thumbnail":["https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/PowerShell-Get-Contents-150x150.jpg",150,150,true],"medium":["https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/PowerShell-Get-Contents-300x169.jpg",300,169,true],"medium_large":["https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/PowerShell-Get-Contents-768x432.jpg",768,432,true],"large":["https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/PowerShell-Get-Contents.jpg",800,450,false],"1536x1536":["https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/PowerShell-Get-Contents.jpg",800,450,false],"2048x2048":["https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/PowerShell-Get-Contents.jpg",800,450,false],"post-thumb":["https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/PowerShell-Get-Contents.jpg",800,450,false],"post-thumb-half":["https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/PowerShell-Get-Contents-400x225.jpg",400,225,true],"wppr-widget":["https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/PowerShell-Get-Contents-50x28.jpg",50,28,true],"gform-image-choice-sm":["https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/PowerShell-Get-Contents.jpg",300,169,false],"gform-image-choice-md":["https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/PowerShell-Get-Contents.jpg",400,225,false],"gform-image-choice-lg":["https:\/\/lazyadmin.nl\/wp-content\/uploads\/2023\/02\/PowerShell-Get-Contents.jpg",600,338,false]},"uagb_author_info":{"display_name":"Rudy Mens","author_link":"https:\/\/lazyadmin.nl\/author\/lajcud8123b\/"},"uagb_comment_info":5,"uagb_excerpt":"The Get-Content cmdlet can be used to retrieve the contents from a file in PowerShell. It will retrieve the contents one line at a time, storing them in an array. This allows you to use or process each line in ... Read moreHow to use PowerShell Get-Content to Read a File","_links":{"self":[{"href":"https:\/\/lazyadmin.nl\/wp-json\/wp\/v2\/posts\/7724"}],"collection":[{"href":"https:\/\/lazyadmin.nl\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lazyadmin.nl\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lazyadmin.nl\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/lazyadmin.nl\/wp-json\/wp\/v2\/comments?post=7724"}],"version-history":[{"count":0,"href":"https:\/\/lazyadmin.nl\/wp-json\/wp\/v2\/posts\/7724\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lazyadmin.nl\/wp-json\/wp\/v2\/media\/7733"}],"wp:attachment":[{"href":"https:\/\/lazyadmin.nl\/wp-json\/wp\/v2\/media?parent=7724"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lazyadmin.nl\/wp-json\/wp\/v2\/categories?post=7724"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lazyadmin.nl\/wp-json\/wp\/v2\/tags?post=7724"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}