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"
	}
}

Maarten

Hi, I am Maarten and I am the owner of this weblog. I post on various IT subjects that matter in my life as an IT professional.

Recent Posts