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

PowerShell Exception of type 'System.OutOfMemoryException'

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

PowerShell "Exception of type 'System.OutOfMemoryException'"
BTW, You can also try disposing of unused objects in PowerShell. Here, we got this exception due to a large number of versions.

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

Leave a Reply

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