January 18th, 2006

How Can I Change the Size of a Page File?

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I change the size of a page file?

— HM

SpacerHey, Scripting Guy! AnswerScript Center

Hey, HM. You know this is a somewhat unusual question for us. Why? Because there’s a very easy and straightforward answer, which doesn’t involve weird workarounds, doesn’t need to be accompanied by a bunch of excuses, and works the same on Windows 2000 as it does on Windows XP and Windows Server 2003. A question that we can just answer without a lot of caveats and what-have-you? How are we supposed to do that?!?

Well, one way might be to just show you the code:

strComputer = “.”
Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)

Set colPageFiles = objWMIService.ExecQuery _ (“Select * from Win32_PageFileSetting”)

For Each objPageFile in colPageFiles objPageFile.InitialSize = 300 objPageFile.MaximumSize = 600 objPageFile.Put_ Next

As you can see, this is a fairly standard WMI script. It starts out by connecting to the WMI service on the local computer, although simply by changing the value of the variable strComputer we would be able to change the page file size on a remote computer. (Just assign strComputer the name of the remote computer.) We then use this line of code to return a collection of all the page files found on the computer:

Set colPageFiles = objWMIService.ExecQuery _
    (“Select * from Win32_PageFileSetting”)

As we noted, this query returns a collection of all the page files found on a computer. Alternatively, we could add a Where clause that returns a specific page file. For example, this query returns only the page file settings for drive C (the file C:\pagefile.sys):

Set colPageFiles = objWMIService.ExecQuery _
    (“Select * from Win32_PageFileSetting Where Name =’C:\\pagefile.sys'”)

Note. No, that’s not a misprint: the file name, at least as far as the Where clause is concerned, is C:\\pagefile.sys. The two \ marks are required because the \ is a reserved character in WMI; hence we have to “escape” the character (by doubling it up) any time we use the \ in a query.

All we have to do now is set up a For Each loop in order to walk through the collection. Inside that loop we use the following lines of code to change two properties of the Win32_PageFileSetting class: InitialSize and MaximumSize. Note that, in each case, these values are expressed in megabytes. Thus this code sets the InitialSize to 300 megabytes and the MaximumSize to 600 megabytes:

objPageFile.InitialSize = 300
objPageFile.MaximumSize = 600
objPageFile.Put_

And, whatever you do, don’t forget that last line of code: the Put_ method is required to actually save the changes and modify the size of the file. Leave out that line of code and the script will run just fine; you won’t get an error message of any kind. However, you won’t change the size of the page file, either. Any time you change the value of a read/write property in WMI (and, yes, InitialSize and MaximumSize are read/write properties) you must use the Put_ method to actually put those changes into effect.

And that’s it, that’s all you have to do. But now what are we supposed to do with all these unused caveats and excuses?

Good point: most likely we will have a use for them sooner or later, won’t we? In fact, now that we think about it, tomorrow’s column could use a few. Ah, the joys of scripting on different versions of Windows ….

Author

2 comments

Discussion is closed. Login to edit/delete existing comments.

  • chetu valligari

    Hi i have a doubt like if i want to set a particular drive as AutomaticManagedPagefile except the C drive is it possible to do that. I need to do this using powershell. Is there a way to do it. Kindly help me on this. My doubt is it really possible to do via powershell. Cmdlets are available for that??

  • chetu valligari

    Hi i have a doubt like if i want to set a particular drive as system managed except the C drive is it possible to do that. I need to do this using powershell. Is there a way to do it. Kindly help me on this. My doubt is it really possible to do via powershell. Cmdlets are available for that??