I am trying to read file of size 1 GB as bytes in powershell using below command,
$data = Get-Content -Encoding byte $filepath
It throws below error : Get-Content : Exception of type 'System.OutOfMemoryException' was thrown.
Searched net and found that running below command fixes it however this is not working for me. set-item wsman:localhost\Shell\MaxMemoryPerShellMB 4096
By default MaxMemoryPerShellMB = 1024MB If you want to read large file say 1GB or greater than that, You need to set MaxMemoryPerShellMB.
Use below code to set MaxMemoryPerShellMB-
function setpsmem
{
$winrmstatus = (get-service WinRM).Status
if ($winrmstatus -eq 'Running')
{
$currentpsmem = (Get-Item WSMan:\localhost\Shell\MaxMemoryPerShellMB).value
if ($currentpsmem -le 4096)
{
Set-Item WSMan:\localhost\Plugin\Microsoft.PowerShell\Quotas\MaxMemoryPerShellMB 4096
Set-Item WSMan:\localhost\Shell\MaxMemoryPerShellMB 4096
Restart-Service winrm
$currentmemory = (Get-Item WSMan:\localhost\Shell\MaxMemoryPerShellMB).value
Write-Host "Script assign $currentmemory[MB] of memory to PowerShell" -ForegroundColor Green
}
else
{
$currentpsmem = (Get-Item WSMan:\localhost\Shell\MaxMemoryPerShellMB) | Select-Object value
Write-Host "Powrshell has $currentpsmem[MB] of memory." -ForegroundColor Green
}
}
else
{
Write-Host "WinRM services is not running on current host." -ForegroundColor Red
}
} setpsmem
Try it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With