Configuring the Page File via WMI (using PowerShell or VBScript)

Configuring the Page File via WMI (using PowerShell or VBScript)

Recently I had to find a way to configure the Page File in a scripted way. When doing some research online found some people suggesting doing this via WMI while others via Registry values. After running some tests I found that the proper way to do this is via WMI, since using Registry settings will only leave the page file configured but the configuration is never made effective, changes will reflect on the graphical interface but with no actual effect.

When you look in WMI side,there are 3 main classes that contain information on how the Page File is configured:

  • Win32_PageFile: deprecated, do not relay on this one!
  • Win32_PageFileSetting: actual configuration of the page file.
  • Win32_PageFileUsage: statistics on page file usage.

So if your page file is configured by default (Image 1) and you try to obtain information from the Win32_PageFileSetting class, you’ll obtain nothing. This happens because as the system is configured to manage the page file size for all drives (Image 1), no instance of the class is created.

(Image 1)

The different configuration statuses you will find are:

  1. No instance of the Win32_PageFileSetting means the system is configured o manage the page file size for all drives. Creating an instance of the class and deleting it won’t return the configuration to this state. Not without a reboot at least.
  2. An instance of the Win32_PageFileSetting class exists with an Initial Size and Maxium Size of ‘0’. This means that the page file(s) will be configured to the specified drive by the size will be system managed.
  3. An instance of the Win32_PageFileSetting class exists with a custom value to the Initial Size and Maxium Size.

This small snippet you can you use to determine the system drive and programatically set the page file to it.


#Get System drive and generate page file name
$SystemDrive=(Get-CimInstance -ClassName Win32_OperatingSystem).SystemDrive

#Create and configure WMI class instance
$PropertiesHashTable = New-Object -TypeName hashtable
$PropertiesHashTable.Add("InitialSize",[uint32]0) #the instance must be initialized with 0
$PropertiesHashTable.Add("MaximumSize",[uint32]0) #the instance must be initialized with 0
New-CimInstance -ClassName Win32_PageFileSetting -Property $PropertiesHashTable | Out-Null
$PropertiesHashTable = New-Object -TypeName hashtable
$PropertiesHashTable.Add("InitialSize",[uint32]1024) #page file min size
$PropertiesHashTable.Add("MaximumSize",[uint32]1024) #page file max size
Set-CimInstance -Query &quot;SELECT * FROM Win32_PageFileSetting WHERE Caption LIKE <code>'%$SystemDrive%</code>'&quot; -Property $PropertiesHashTable


'Get System drive and generate page file name
set colSystemDrive=objWMIService.ExecQuery("Select SystemDrive from Win32_OperatingSystem")

'Create and configure WMI class instance
Set classPageFile = objWMIService.Get("Win32_PageFileSetting")
Set instancePageFile = classPageFile.SpawnInstance_
instancePageFile.InitialSize="0" 'the instance must be initialized with 0
instancePageFile.MaximumSize="0" 'the instance must be initialized with 0
instancePageFile.InitialSize='page file min size
instancePageFile.MaximumSize='page file max size

Extra tips

Here are some things I learned while dealing with my script that might also be useful to you. Or might be even the main reason why you got to this article.

WMI Discovery

When dealing with VBScript and WMI, wbemtest is a powerful tool. When you are lost, it might give you a hint on how to continue. For example, it was quite easy to instantiate a WMI class via PowerShell, but when it came up to do it with VBScript, I had no clue how to do that. But when playing with webemtest found in the window where you can create a new instance (Image 2) the hint “SpawnInstance”. And with some googling afterwards it led me to the proper methods to be used on my VB script.

(Image 2)

PowerShell and WMI

When using PowerShell to deal with WMI, ALWAYS use CIM cmdlets, not the old WMI ones. Here (Image 3) you can appreciate how many more cmdlets have been included in the CimCmdlets module in contrast with the old ‘WMIObject’ cmdlets.

(Image 3)

The new CIM cmdlets (besides the differences they introduce vs WMIObjects cmdlets when working remotely), they main thing they have introduced is the flexibility to work with classes and instances. For example, I was trying to delete an instance of an object, so I did:

Get-WmiObject -Class Win32_PageFileSetting | Remove-WMIObject

And ended up deleting all the instances but also the class itself. While by doing:

Get-CimInstance -Class Win32_PageFileSetting | Remove-CIMInstance

The instances of a given class would be deleted, while the class remains intact.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.