Are your Powershell scripts taking too lengthy to run and at all times timeout? PowerShell is commonly used to automate duties or to configure methods reminiscent of Trade. Nonetheless, using Powershell is usually very useful resource or reminiscence intensive, e.g. for file operations. The execution of Trade cmdlets for the administration of Trade OnPrem and Trade On-line/M365 can even consequence within the PowerShell course of utilizing numerous reminiscence. The reminiscence used may be within the gigabyte vary.
Typical error dealing with and its pitfalls
For those who shut the PowerShell course of, (e.g. by closing the PowerShell shell, PowerShell console), the occupied RAM ought to rapidly be launched. However what occurs if we execute additional PowerShell instructions or advanced scripts that decision varied cmdlets in the identical PowerShell course of? Then the reminiscence consumption continues to extend, which ends up in the system changing into slower and slower. By the way, this behaviour not solely impacts Home windows methods, but in addition Linux and macOS. Nonetheless, Linux and macOS are solely supported from PowerShell model 6 onwards.
So how can we unlock RAM?
So far as I do know, there are 3 methods to do that, which may also be mixed with one another.
Use the Rubbish collectors
DPowerShell relies on the .NET Frequent Language Runtime (CLR). Which means, relying on the working system, all or virtually all choices of the .NET Runtime are additionally obtainable below PowerShell.
One of many benefits of .NET Runtime is that it robotically frees up RAM that’s now not required and keep away from PowerShell timeout. That is carried out by the Rubbish Collector (GC).
Nonetheless, we’ve got no direct affect on the automated clean-up, i.e. this may happen instantly or at a later stage.
If we need to request the clean-up of the reminiscence in its scripts instantly, we name the command “[System.GC]::Accumulate();”.
For additional data on the rubbish collector, the Microsoft documentation is useful: https://study.microsoft.com/en-us/dotnet/commonplace/garbage-collection/
Redirection of bills to $null
Many PowerShell cmdlets return an output that’s handed on to the PowerShell host, often the PowerShell console. This additionally results in additional reminiscence utilization. If the output just isn’t required, we will redirect it to $null.
There are 3 choices for this:
Use of the Cmdlets Out-Null
The Out-Null cmdlet forwards an output to NULL. That is deleted from the pipeline and can be not displayed.
Join–ExchangeOnline | Out–Null |
You could find additional data at Microsoft: https://study.microsoft.com/en-us/powershell/module/microsoft.powershell.core/out-null?view=powershell-7.4
Redirection to $null
To redirect an output to NULL, the redirection operator “>” may also be used.
Join–ExchangeOnline $null |
Task to $null
A 3rd possibility is that we assign the outcomes of a command to $null.
$null= Join–ExchangeOnline |
elete variables which are now not required
As in different programming languages, we regularly use variables in PowerShell scripts. Reminiscence can be reserved for variables and their content material. We are able to additionally unlock RAM when the variables are now not required and keep away from PowerShell timeout.
We are able to use the “Take away-Variable” or “Clear-Variable” cmdlets to do that, or we will merely assign the worth $null to the variable.
Nonetheless, there may be additionally a particular function right here, as we have to know.
The next state of affairs ought to serve for instance:
A variable $textual content with the content material “Hiya world” is outlined.
Set the variable to $null
We are able to set a variable to null by writing:
The particular function right here is that we don’t delete the variable. We solely delete the content material of the variable. The variable itself and due to this fact its reminiscence reservation stays.
We are able to examine this by getting into the Get-Variabletext command. The variable identify is written with out the main $.
If we execute this command, we get:

Utilizing the Clear-Variable cmdlet
We are able to additionally use the Clear-Variable cmdlet to delete the worth of the variable. Nonetheless, the variable itself can be retained right here.
The variable identify can be written right here with out the main $.
Utilizing the Take away-Variable cmdlet
The popular method to delete a variable, and thus unlock its RAM, is to make use of the Take away-Variable.
The variable identify can be written with out the main $.
If we now name the Get-Variable textual content command, an exception is thrown because the variable now not exists within the system.

Keep away from PowerShell timeout when accessing Trade
To handle Trade OnPrem or Trade On-line, PowerShell should even be used, as solely an element may be managed through the administration interfaces.
Trade On-line
To hook up with Trade On-line, a connection should first be established utilizing the Cmdlet Join-ExchangeOnline.
$null= Join–ExchangeOnline |
This cmdlet additionally returns an output that we will assign to $null if it isn’t wanted. As well as, this cmdlet additionally creates tmpEXO information within the temp listing of the pc (%localappdatapercenttemp) on which the cmdlet was referred to as.
If we ceaselessly use Join-ExchangeOnline in a script, new tmpEXO information are at all times created. To stop this, we must always at all times use
$null= Disconnect–ExchangeOnline –Verify:$false |
With this cmdlet we finish the Trade session once more.
(https://study.microsoft.com/en-us/powershell/module/alternate/disconnect-exchangeonline?view=exchange-ps)
As we will see, the output of the cmdlet is once more assigned to $null. The “-Verify” parameter is barely used to specify whether or not a affirmation immediate must be displayed or not. Within the instance above, no affirmation immediate is displayed.
Even when we are not looking for it, errors can happen when operating PowerShell. If these happen, for instance, after calling Join-ExchangeOnline and no appropriate error dealing with has been applied, the Trade session stays open. Which means the non permanent information are nonetheless obtainable.
It due to this fact is sensible to put Join-ExchangeOnline in a strive..catch..lastly assertion. The decision of Disconnect-ExchangeOnline ought to happen within the lastly block, as that is at all times run by way of, i.e. even when an error happens.

Within the instance, the decision to Disconnect-ExchangeOnline was additionally positioned in a separate try-catch assertion. If an error happens when calling Disconnect-ExchangeOnline, we will log this and forestall an uncontrolled cancellation of the code or script.
Trade OnPrem
To attach remotely to Trade OnPrem, a distant PowerShell session is used. To do that, the three cmdlets are referred to as one after the opposite:
$credential= Get–Credential
$psSession= New–PSSession –ConfigurationNameMicrosoft.Trade –ConnectionUri
http://ServerFQDN/PowerShell/ -Authentication Kerberos -Credential $credential
Import–PSSession $psSession –DisableNameChecking |
The Trade cmdlets can be utilized to regulate Trade OnPrem.
If entry to Trade OnPrem is now not required, we must always launch the session and likewise the variables from the reminiscence.
The Take away-PSSession cmdlet known as to finish the session:
Take away–PSSession –Id $psSession |
This cmdlet closes the present PowerShell session, stops all instructions which are executed on this session, and releases the sources used. The variables used also needs to be deleted once more utilizing the Take away variable.
When utilizing the Trade On-line and Trade OnPrem cmdlets, it’s also greatest to redirect all cmdlets that return an output and that we don’t have to $null with a purpose to save sources.
Conclusion
To keep away from PowerShell timeout, sources should be saved. There are numerous methods to do that. Despite the fact that reminiscence and laborious disk house are comparatively cheap today, we must always nonetheless attempt to use them fastidiously.
On this article, I’ve listed the methods of saving sources that I do know and use myself. These can be utilized individually or together and are comparatively simple to use.
FirstAttribute AG – Id & Entry Administration and IAM Cloud Providers
We’d be blissful to current our software program options to you.
Get in contact and learn how we can assist you.
Did this show you how to? Share it or go away a remark:
Article created: 08.07.2024