Get-Content loads the entire file content into RAM to be acted upon.
You need to upgrade your RAM
Use a deferent method and there are a few of them.
Get-Content
Get-Content -Raw
Get-Content -ReadCount
Switch -File
The .Net reader is the most optimal
[System.IO.File]::ReadAllText()
[System.IO.File]::ReadAllLines()
[System.IO.File]::ReadLines()
[System.IO.File]::OpenText().readtoend()
System.IO.File.ReadLines() is more than likely your best choice as it returns all the lines of a file, but lets you begin iterating over the lines immediately which means it does not have to store the entire contents in memory.
More details here: https://msdn.microsoft.com/en-us/library/dd383503.aspx
Requires .NET 4.0 or higher.
foreach ($line in [System.IO.File]::ReadLines($filename)) {
# do something with $line
}
So, you could do something like this...
$reader = [System.IO.File]::OpenText("my.log")
try {
for() {
$line = $reader.ReadLine()
if ($line -eq $null) { break }
# process the line
$line
}
}
finally {
$reader.Close()
}
Or shorten it to this...
$reader = [System.IO.File]::OpenText("my.log")
while($null -ne ($line = $reader.ReadLine())) {
$line
}