Change pagefile settings on multiple servers using Powershell

Today I wanted to change the page file settings on some of our servers (about 80), doing that by hand would take ages.
That’s why I came up with the following Powershell script, the script does the following:

  • Loop through a list of Windows 2003 servers in our active directory, only Windows 2003 machines were needed. You can easily modify the search filter though.
  • Set pagefile size on those servers, minimum size and maximum size set to “0” means the pagefile settings will be set on “system managed”

So, here’s the full source of the script:

# Ask for credentials to do remote WMI.
$cred    	= Get-Credential DOMAIN\Administrator

# Filter used for active directory query. Only Windows 2003 is needed, Windows 2008 defaults to system managed pagefile size.
$strFilter 	= "(&(objectClass=computer)(operatingSystem=Windows Server 2003))"

$objDomain 	= New-Object System.DirectoryServices.DirectoryEntry
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = "Subtree"

$colResults = $objSearcher.FindAll()

# Loop through list of servers.
foreach ($objResult in $colresults)
{
	$objItem = $objResult.Properties
	
	$name = $objItem.name[0]

	Write-Host "Setting pagefile on server: $name"
	
	try 
	{
		$PageFile = Get-WmiObject Win32_PageFileSetting -Credential $cred -ComputerName $objItem.name
		$PageFile.InitialSize = 0
		$PageFile.MaximumSize = 0
		$PageFile.Put()
	} 
	catch 
	{
		Write-Host "Failed to set pagefile on: $name, no permission or server down?"
		break
	}
	finally 
	{
		Write-Host "Pagefile set on: $name"
	}
}
You can leave a response, or trackback from your own site.

3 Responses to “Change pagefile settings on multiple servers using Powershell”

  1. […] Today found this great post, here is a quick excerpt : Powershell script for changing pagefile settings on multiple servers. February 19th, 2010 No Comments. Today I wanted to change the page file settings on some of our servers (about 80), doing that by hand would take ages. … Read the rest of this great post Here […]

  2. Albert Widjaja says:

    Hi,

    Is it possible to modify the script to view the current swap file settings first ?

    Thanks for sharing.

  3. therealrory says:

    here’s a script to check the page file size/

    clear
    $strComputer=””

    $PageFile=Get-WmiObject Win32_PageFile -ComputerName $strComputer
    Write-Host “Page File Size in MB: ” ($PageFile.Filesize/(1024*1024))

    $colItems=Get-WmiObject Win32_PhysicalMemory -Namespace rootCIMv2 -ComputerName $strComputer
    $total=0
    foreach ($objItem in $colItems)
    {
    $total=$total+ $objItem.Capacity
    }

    $isPAEEnabled =Get-WmiObject Win32_OperatingSystem -ComputerName $strComputer

Leave a Reply