PowerShell “Exception of type ‘System.OutOfMemoryException'”
In the middle of script execution, I 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
Solution:
Increase the PowerShell console memory! Here is how:
C:\
CD wsman:
#Get the current setting
Get-Item localhost\shell\maxmemoryperShellMB
#Increase the memory
Set-Item localhost\shell\MaxmemoryPerShellMB 2048
BTW, You can also try disposing of unused objects in PowerShell. Here, we got this exception due to a large number of versions.