Powershell Script running is showing error
Expected Behavior When we run Powershell script in Powershell ISE in Sitecore. Then it should generate the desired output/result.
Actual Behavior In Sitecore 9.2 environment, when we are running Powershell script on PowerShell console. After some time, we are seeing one exception and script execution is failing.
Steps to Reproduce the Problem We copy the script and open Powershell ISE and execute the script then after some time we see below error:
Exception of type: 'System.OutOfMemoryException' was thrown, at Line: 60 Char: 21
And it is happening with every script.

Sitecore Version: 9.2 Powershell Version: 5.1
The line throwing the exception doesn't appear in your screenshot. My guess is that the query is quite broad and the machine (local?) runs out of memory. What is the value of your scope query and how many items does that match?
Please let me know if any other information is required to investigate the issue
Get-Contacts and Get-Value appear to be custom functions. What do they do?
Can you provide a more complete example of your script? The screenshots do not provide much context into what is happening in the script.
Apologies. the script is different. We are getting this error:
and the line(69) throwing the exception are as below:
This issue is occurring only on production. If we run the same script on any lower environments then it works as expected. Please let me know if anything else is required to investigate the issue.
Please let me know if there is anything else required to investigate the issue.
One idea I had over the weekend was to remove the call to Initialize-Item (and everything after) to see if the call to Find-Item completes. See if you can get a count of the items returned. My guess would be that the scope query is far too broad and you eating up your server memory. There are ways to paginate with Find-Item.
There are no clear reproduction steps that would allow us to fix this issue. Please provide script that will cause the issue on a clear Sitecore instance, otherwise we have to assume it's 3rd party code that is causing it.