Pagefile For Exchange 2013 / 2016 – Multiple Servers

Earlier this week, I published an article on configuring the Pagefile on an Exchange server to the correct size. The script was intended to illustrate how to accomplish this on the current server. The intent of this article is to take the same base script for a single server and apply it to all Exchange Servers. In order to make that work, we need to get a list of Exchange Servers to apply the changes to and then feed the list of servers through a Foreach loop to run the basic script against each Exchange Server. Additionally, the some lines that use Get-CIMInstance and Set-CIMInstance need to have the ‘-ComputerName’ parameter added along with the name of the server from the current iteration of the loop.

Lines To Be Added

$Servers = (Get-ExchangeServer).name
Foreach ($Server in $Servers) {
     (Insert single server code here)

Add this ‘-computername $ComputerName’ to all CIM based cmdlets.

Final Script

Here is the complete script:

# Clear the screen

# Variables
$Servers = (Get-ExchangeServer).name

Foreach ($Server in $Servers) {
    Write-Host "Configuring the Pagefile on $Server......" -ForegroundColor Green
    Write-Host " "
    Start-Sleep 5

    # Loop Specific Variables
    # $Computername = $env:computername
    $Computername = $Server

    # Get RAM and set ideal PageFileSize
    try {
        [int32]$RamInMb = (Get-CIMInstance -computername $ComputerName -Classname win32_physicalmemory -ErrorAction Stop | measure-object -property capacity -sum).sum/1MB
    } catch {
        write-host "Cannot acquire the amount of RAM in the server." -ForegroundColor Red
        $stop = $true
    [int32]$Size = $RAMinMb + 10

    # Set Maximum Pagefile Size
    If ($Size -gt 32778) {
        $Size = 32778

    # Disk Space Check
    $Disk = Get-WmiObject Win32_LogicalDisk -ComputerName $ComputerName -Filter "DeviceID='C:'" | Select-Object Size,FreeSpace
    $MB = $Disk.FreeSpace/1mb
    $FreeSpace = [Math]::Round($MB)

    if ($FreeSpace -lt $Size) {
        Write-Host "There isn't enough diskspace available for configuring the Pagefile to the correct size." -ForegroundColor Yellow
        Write-Host "The Pagefile needs to be $Size MB and the amount of freespace is $FreeSpace MB." -ForegroundColor White
    } Else {

        Write-Host " "
        Write-Host "The Pagefile will be set to a Maximum size of $Size MB and a Minimum size of $Size MB." -ForegroundColor Yellow
        Write-Host " "

        # Option 1
        # Set-CimInstance -Property @{AutomaticManagedPageFile = $False}

        # Set the Automatic Pagefile to False
        Set-CimInstance -ComputerName $ComputerName -Query “Select * from win32_computersystem” -Property @{automaticmanagedpagefile=”False”}  -ErrorAction STOP

        # Remove the old Pagefile
        $PageFile = Get-CimInstance -ClassName Win32_PageFileSetting -ComputerName $ComputerName
        $PageFile | Remove-CimInstance

        # Set the Pagefile to the new size
        New-CimInstance -ComputerName $ComputerName -ClassName Win32_PageFileSetting -Property  @{Name= "$("C"):\pagefile.sys"}
        Get-CimInstance -ComputerName $ComputerName -ClassName Win32_PageFileSetting | Set-CimInstance -Property @{InitialSize = $size; MaximumSize = $size}

        # End Result
        $pagefile = Get-CimInstance -ComputerName $ComputerName win32_PageFileSetting -Property * | select-object Name,initialsize,maximumsize
        $name = $;$max = $pagefile.maximumsize;$min = $pagefile.initialsize
        write-host " "
        write-host "The Pagefile of $Name on $Server is now configured for an initial size of " -ForegroundColor white -NoNewline
        write-host "$min " -ForegroundColor green -NoNewline
        write-host "and a maximum size of " -ForegroundColor white -NoNewline
        write-host "$max." -ForegroundColor Green
        write-host " "
        Write-Host "DON'T FORGET TO REBOOT THE $Name SERVER!!!" -ForegroundColor Red
    Write-Host " "
    Write-Host "Completed the progress for Pagefile configuration on $Server......" -ForegroundColor Cyan
    Start-Sleep 5
    Write-Host " "
    Write-Host " "

Script Run-Through
A quick run-through of the script shows one failed configuration and one successful configuration of the Pagefile for multiple servers:

One More addendum
The above code will only handle Exchange Servers that do not have the Edge Transport role installed on it. I will cover how to handle those servers in an additional article later this month.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s