Jeff Wouters's Blog

Out of memory exception in PowerShell

by on Mar.04, 2014, under DuPSUG, PowerShell, Windows, Windows Server

When running scripts in a large environment, you could run into an exception in PowerShell telling you it’s out of memory.

PS-OutOfMemory

To get the value of the amount of memory available per shell, run the following command:

PS-GetMaxMemoryPerShellMB

The PowerShell MaxMemoryPerShellMB quota defaults to only 150MB inΒ v1 and v2, and 1024MB in v3.
But even under PowerShell 3.0 large objects can consume large amounts of memory.

You can resolve the ‘out of memory’ exception simply by increasing the value of MaxMemoryPerShellMB.
To accomplish this, use the following command:

PS-SetMaxMemoryPerShellMB

As shown in the screenshot above, you can run the Get-Item command again to verify the command.

Last: Restart the winrm service… and you’re done πŸ™‚


3 Comments for this entry

Leave a Reply