PowerShell "Exception of type 'System.OutOfMemoryException'"

In middle of a script execution, Got the below error:
The following exception was thrown when trying to enumerate the collection: "Exception of type 'System.OutOfMemoryException' was thrown.".
At d:\Scripts\VersionRpt.ps1:19 char:16
+         foreach <<<<  ($FileVersion in $File.Versions)
    + CategoryInfo          : NotSpecified: (:) [], ExtendedTypeSystemException
    + FullyQualifiedErrorId : ExceptionInGetEnumerator
PowerShell Exception of type 'System.OutOfMemoryException'

Increase the PowerShell console memory! Here is how:

CD wsman:

#Get the current setting
Get-Item localhost\shell\maxmemoryperShellMB

#Increase the memory
Set-Item localhost\shell\MaxmemoryPerShellMB 2048
PowerShell "Exception of type 'System.OutOfMemoryException'"
BTW, You can also try disposing unused objects in PowerShell. Here, we got this exception due to the large number number of version.
PowerShell "Exception of type 'System.OutOfMemoryException'" PowerShell "Exception of type 'System.OutOfMemoryException'" Reviewed by Salaudeen Rajack on July 16, 2014 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.