How to adjust call depth in PowerShell?

I was just trying to do something in PowerShell and got an error stating that the call depth was set to 1000 in some test recursive function. I looked at the information on the Internet and found that this was due to error handling in PowerShell (if I understood correctly):

The recursion depth limit is fixed in version 1. Deep recursion caused problems in 64-bit mode because exceptions were handled. This caused cascading errors out of memory. As a result, we have limited the recursion depth on all platforms to ensure that scripts are portable across all platforms. - Bruce Payet, PowerShell Co-Designer

I found here .

I also found this exception page on MSDN, which states that this limit is configurable (but I haven't found anything about how to do this) - see the comments section here .

How to set this limit?

+4
exception powershell recursion
source share
1 answer
  • In PowerShell V1, the maximum call depth is 100:

Using the .NET Reflector , we can see in this fragment the code of the System.Management.ExecutionContext class,

 internal int IncrementScopeDepth() { using (IDisposable disposable = tracer.TraceMethod("{0}", new object[] { this.scopeDepth })) { if (this.CurrentPipelineStopping) { throw new PipelineStoppedException(); } this.scopeDepth++; if (this.scopeDepth > 100) { ScriptCallDepthException exceptionRecord = new ScriptCallDepthException(this.scopeDepth, 100); tracer.TraceException(exceptionRecord); throw exceptionRecord; } return this.scopeDepth; } } 

that it is impossible to change hardcoded 100 .

  • In PowerShell V2, the maximum call depth is 1000

Again, looking at the code, it seems that it does not fit the default maximum call depth.

  • In PowerShell V3 (CTP) there is no maximum call depth (if you do not have enough resources, of course). This behavior has been described as a connection error , so it may change in the final version.
+4
source share

All Articles