Introduction
Windows Server administrators often need to configure various parameters to optimize PowerShell session performance and resource allocation. One such critical parameter is the MaxMemoryPerShellMB, which determines the maximum memory allocated to each PowerShell session. This article will explore multiple methods to configure the MaxMemoryPerShellMB setting.
Method 1: Using PowerShell Command Line Interface
The most straightforward way to configure MaxMemoryPerShellMB is through PowerShell itself. Follow these steps:
- Open a PowerShell command line interface.
- Navigate to the WSMan Shell configuration by using the sl (
Set-Location
) and dir (Get-Item
) commands:
PS C:\> sl WSMan:\localhost\Shell
PS WSMan:\localhost\Shell> dir
- Locate the
MaxMemoryPerShellMB
setting in the configuration. It should be displayed along with its current value. - To set a new value, use the
Set-Item
command:
PS WSMan:\localhost\Shell> Set-Item .\MaxMemoryPerShellMB 1024
This method allows for precise control of the MaxMemoryPerShellMB
setting.
Method 2: Using PowerCLI Commands
If you prefer to use PowerCLI commands for configuring PowerShell memory, here’s how to do it:
- Open a PowerShell command line interface.
- To check the current value of MaxMemoryPerShellMB, use the following command:
Get-Item WSMan:\localhost\Shell\MaxMemoryPerShellMB
- To set a new value, use the Set-Item command as shown below:
Set-Item WSMan:\localhost\Shell\MaxMemoryPerShellMB 1024
These PowerCLI commands provide an alternative way to configure the MaxMemoryPerShellMB setting with a more PowerShell-centric approach.
Method 3: Using Batch Command
Configuring MaxMemoryPerShellMB can also be achieved using a batch command. Follow these steps:
- Open a Command Prompt (CMD).
- Run the following command to configure
MaxMemoryPerShellMB
:
winrm.cmd set winrm/config/winrs @{MaxMemoryPerShellMB="1024"}
This method is useful if you prefer to work with batch scripts or are in an environment where batch commands are more prevalent.
Method 4: Configuring Multiple Machines Remotely
If you need to configure MaxMemoryPerShellMB on multiple machines remotely, you can use a batch command as well. Here’s how:
- Open a Command Prompt (CMD).
- Run the following command, assuming you have a list of target servers in a file named
servers.txt
:
for /f %%i in (servers.txt) do (
echo %%i
psexec \\%%i -s winrm.cmd quickconfig -q
psexec \\%%i -s winrm.cmd set winrm/config/winrs @{MaxMemoryPerShellMB="512"}
)
This command will configure MaxMemoryPerShellMB on all the servers listed in servers.txt remotely.
Conclusion
In conclusion, configuring the MaxMemoryPerShellMB setting is essential for managing PowerShell sessions efficiently, and you can choose the method that best suits your requirements and environment. Whether you prefer PowerShell, PowerCLI, batch commands, or remote configurations, these methods provide flexibility and control over your Windows Server PowerShell sessions.
Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.Academy
Learn the Ansible automation technology with some real-life examples in my Udemy 300+ Lessons Video Course.
My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Donate
Want to keep this project going? Please donate