Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8677513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:28:30+00:00 2026-06-12T20:28:30+00:00

In PowerShell, the normal way of redirecting standard input to a file is to

  • 0

In PowerShell, the normal way of redirecting standard input to a file is to pipe the contents of the file:

Get-Content input-file.txt | Write-Host

However, if the file is very large, PowerShell begins to consume a large amount of memory. Using a small -ReadCount seems to speed up how quickly Get-Content starts feeding rows into the command, but the memory consumption is still large.

Why is the memory usage so high? Is it that PowerShell is retaining the contents of the file in memory, even though it doesn’t need to? Is there some way to mitigate that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T20:28:31+00:00Added an answer on June 12, 2026 at 8:28 pm

    The following function will read the file in line by line by using the .NET StreamReader class and send each line down the pipeline. Sending this to Out-Null my memory usage only went up by a few 10’s of KB while it was executing on a nearly 2,000,000 line log file (~186 MB):

    function Get-ContentByLine {
      param (
        [Parameter(Mandatory=$true,ValueFromPipeline=$true)][PsObject]$InputObject
      )
    
      begin {
        $line = $null
        $fs = [System.IO.File]::OpenRead($InputObject)
        $reader = New-Object System.IO.StreamReader($fs)
      }
    
      process {
        $line = $reader.ReadLine()
        while ($line -ne $null) {
            $line
            $line = $reader.ReadLine()
        }
      }
    
      end {
        $reader.Dispose();
        $fs.Dispose();
      }
    }
    

    You would invoke it like this:

    PS C:\> Get-ContentByLine "C:\really.big.log" | Out-Null
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

PowerShell provides a simple technique to view the contents of a function, e.g. Get-Content
With powershell 2.0: write-output abcd >> mytext.txt returns: a nul b nul c nul
PowerShell snippet: Import-Module Pscx Expand-Archive ConsoleApplication1.zip ./ Write-Host $? Write-Host $LastExitCode Neither $? nor
My PowerShell script file is located in C:/this-folder/that-folder/another-folder/powershell-file.ps1 . How do I get a
Basic powershell question here - I have two computers in the c:\temp\servers.txt file and
PowerShell's type extension facility is neat, but I haven't yet figured out the way
In PowerShell you can use the Get-WmiObject cmdlet to grab WMI classes. I have
Using powershell how do you create a content sorce that uses a BDC? Documentation
I have a simple module called MyModule.psm1 defined as function Show-Text($p) { Write-Host $p
In Powershell, how do I redirect standard output to standard error ? I know

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.