Out of memory exception in PowerShell

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

  1. megamorf. says:

    From the steps required to apply the newly set value I assume it’s a permanent change. And if I happen to run into the out of memory error in a remoting session I’d have to change the MaxMemoryPerShellMB on the remoting target, right?

  2. Jeff Wouters says:

    Hi Megamorf,
    Take a look at this excelent post by Ed Wilson (MSFT) https://blogs.technet.com/b/heyscriptingguy/archive/2013/07/30/learn-how-to-configure-powershell-memory.aspx
    Explains it πŸ™‚
    Jeff.

  3. Jeff Wouters says:

    Hi Megamorf,
    Take a look at this excelent post by Ed Wilson (MSFT) https://blogs.technet.com/b/heyscriptingguy/archive/2013/07/30/learn-how-to-configure-powershell-memory.aspx
    Explains it πŸ™‚
    Jeff.

Leave a Reply

Your email address will not be published. Required fields are marked *