AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Four Methods to Configure Maximum PowerShell Memory in Windows Server

By Luca Berton · Published 2024-01-01 · Category: installation

Learn how to configure the MaxMemoryPerShellMB setting in PowerShell to optimize session performance using command line and PowerCLI methods for Windows Server.

Four Methods to Configure Maximum PowerShell Memory in Windows Server

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.

See also: Configure WSL in a Domain Environment: Step-by-Step Guide

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.

See also: Quota Management for WinRM Remote Shells

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.

See also: Tunneling WinRM via SSH with PSRP

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.

Related Articles

Ansible for Windows guide

Category: installation

Browse all Ansible tutorials · AnsiblePilot Home