PageFile vbs for Vista and Win7 systems

Windows 7 Box,domain logon,admin pack,taskbar thumbnails,task scheduler,preview pane,windows 7,chrome theme,icon cache,admin shares,install the printer,drives

A good friend of mine wrote up a very small vbscript to handle paging file creation/deletion/modification on Vista/Win7 systems (as PageFileConfig.vbs doesn’t exist inbox, and it didn’t have the ability to handle some of the situations that can arise during setup), and it ended up morphing into this.

Script might work on Windows XP/2003 system, but it’s not tested yet.

He thinks that it would save someone the time if they had a need for it, so here it is:

'// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ '// '// NAME:        setpagefile.vbs '// '// Original:    http://www.cluberti.com/blog '// Last Update: 16th June 2010 '// '// Comment:     VBS example file for use in configuring the paging file on Windows '// '// NOTE:        Provided as-is - usage of this source assumes that you are at the '//              very least familiar with the vbscript language being used and '//              the tools used to create and debug this file. '// '//              In other words, if you break it, you get to keep the pieces. '// '// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  Option Explicit Dim strComputer, strPageFileSizeInput, strPageFileNameInput, strInput Dim objWMIService, objStaticPageFileItem, objDynamicPageFileItem, objShellApp Dim colStaticPageFileItems, colDynamicPageFileItems, isRunning, execOutput, gErrData, bRebootNeeded, bNoStaticPageFile, bNoPageFile  '// Ensure that cscript is the engine used to run this script: RunMeWithCScript()  On Error Resume Next  '// Configure our variables: strComputer = "." bRebootNeeded = False bNoStaticPageFile = True bNoPageFile = True  Set objShellApp = CreateObject("WScript.Shell") Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") '// Win32_PageFile is deprecated - it still (mostly) works, but Win32_PageFileSetting is not deprecated, so use that: Set colStaticPageFileItems = objWMIService.ExecQuery("SELECT * FROM Win32_PageFileSetting",,48) Set colDynamicPageFileItems = objWMIService.ExecQuery("SELECT * FROM Win32_PageFileUsage",,48)  '// Execute the ChangePagingFile sub: ChangePagingFile()  '// Check execution of ChangePagingFile(), and handle when "system managed" is set, or no paging file exists: If bRebootNeeded = True Then     RebootSystem() Else     '// Can't use WMI to create a paging file when it is "system managed" or none exists - handle this condition:     If bNoStaticPageFile = True Then         CheckForDynamicPagingFile()         If bNoPageFile = False Then             WScript.Quit         Else             If bNoPageFile = True Then                 WScript.Echo "No paging file found."                 WScript.Echo ""                 strPageFileNameInput = UserInput("Enter new pagefile name (full path and filename) - leave blank to use C:\pagefile.sys: ")                 strPageFileSizeInput = UserInput("Enter new pagefile size (in MB) - leave blank to keep the system with no paging file:  ")                                 '// Check to see if the user left the input blank, otherwise set up a paging file:                 If Not strPageFileSizeInput = "" Then                     If strPageFileNameInput = "" Then                         strPageFileNameInput = "C:\pagefile.sys"                     End If                     WScript.Echo "New pagefile file will be: " & strPageFileNameInput                     WScript.Echo "New pagefile size will be: " & strPageFileSizeInput & "MB"                     bNoStaticPageFile = False                                         '// Change the registry to add paging file information:                     objShellApp.Exec("reg add ""HKLM\System\CurrentControlSet\Control\Session Manager\Memory Management"" /v PagingFiles /t REG_MULTI_SZ /d """ & strPageFileNameInput & " " & strPageFileSizeInput & " " & strPageFileSizeInput & """ /f")                                         '// Check the return to make sure we succeeded - otherwise, tell the user why we didn't:                     If Not Err.Number = 0 Then                         gErrData = gErrData & vbCrLf & "Error writing to registry - reason: " & Err.Number & "  - " & Err.Description                         WScript.Echo gErrData                         Err.Clear                     Else                         WScript.Echo "Registry edit to add " & strPageFileNameInput & " as size " & strPageFileSizeInput & "MB was successful."                         bRebootNeeded = True                     End If                                 '// If we've made changes that have succeeded, we need to reboot - otherwise, do nothing:                     If bRebootNeeded = True Then                         RebootSystem()                     Else                         WScript.Echo "No changes made that require a reboot.  Exiting script."                     End If                 Else                     WScript.Echo "You have elected to continue with no paging file.  Exiting script."                 End If             Else                 WScript.Echo "No changes made that require a reboot.  Exiting script."             End If         End If     End If End If Sub ChangePagingFile()     For Each objStaticPageFileItem In colStaticPageFileItems         '// There's a static paging file - use WMI to make changes:         bNoStaticPageFile = False         bNoPageFile = False                 WScript.Echo " --------------------------"         WScript.Echo "  Current PageFile Details"         WScript.Echo " --------------------------"         WScript.Echo "Name:                " & objStaticPageFileItem.Name         WScript.Echo "InitialSize:         " & objStaticPageFileItem.InitialSize         WScript.Echo "MaximumSize:         " & objStaticPageFileItem.MaximumSize         WScript.Echo ""                 '// Get user input:         strInput = UserInput("Enter new pagefile size (in MB) - leave blank to delete existing paging file: ")                 '// If the user entered a value, configure the new paging file size:         If Not strInput = "" Then         WScript.Echo "New pagefile size will be: " & strInput & "MB"             WScript.Echo ""                         objStaticPageFileItem.InitialSize = strInput             objStaticPageFileItem.MaximumSize = strInput             objStaticPageFileItem.Put_             bRebootNeeded = True                         WScript.Echo " --------------------------"             WScript.Echo "    New PageFile Details"             WScript.Echo " --------------------------"             WScript.Echo "Name:                " & objStaticPageFileItem.Name             WScript.Echo "InitialSize:         " & objStaticPageFileItem.InitialSize             WScript.Echo "MaximumSize:         " & objStaticPageFileItem.MaximumSize             WScript.Echo ""             WScript.Echo "A reboot is needed for this change to take effect."                 '// If the user left the input blank, delete the paging file - note that Delete and DeleteEx from Win32_PagingFile are deprecated, and         '// while they'll return 0 on Windows 7, they don't actually delete the paging file.  Using reg add instead, as it works just fine:         Else             WScript.Echo "Deleting paging file: " & objStaticPageFileItem.Name             objShellApp.Exec("reg add ""HKLM\System\CurrentControlSet\Control\Session Manager\Memory Management"" /v PagingFiles /t REG_MULTI_SZ /d """" /f")                         '// Check the return to make sure we succeeded - otherwise, tell the user why we didn't:             If Not Err.Number = 0 Then                 gErrData = gErrData & vbCrLf & "Error writing to registry - reason: " & Err.Number & "  - " & Err.Description                 WScript.Echo gErrData                 Err.Clear             Else                 WScript.Echo "Registry edit to remove " & objStaticPageFileItem.Name & " was successful."                 bRebootNeeded = True             End If         End If     Next End Sub Sub CheckForDynamicPagingFile()     For Each objDynamicPageFileItem In colDynamicPageFileItems         '// There's a paging file - use WMI to make changes:         bNoPageFile = False                 WScript.Echo "Paging file is configured for 'System managed size'"         WScript.Echo ""          WScript.Echo " --------------------------"         WScript.Echo "  Current PageFile Details"         WScript.Echo " --------------------------"         WScript.Echo "Name:                " & objDynamicPageFileItem.Name         WScript.Echo "Size:                " & objDynamicPageFileItem.AllocatedBaseSize         WScript.Echo "Current Usage:       " & objDynamicPageFileItem.CurrentUsage         WScript.Echo ""                 WScript.Echo ""         strPageFileNameInput = UserInput("Enter new pagefile name (full path and filename) - leave blank to use C:\pagefile.sys: ")         strPageFileSizeInput = UserInput("Enter new pagefile size (in MB) - leave blank to remove the paging file:               ")                 '// Check to see if the user left the input blank, otherwise set up a paging file:         If Not strPageFileSizeInput = "" Then             If strPageFileNameInput = "" Then                 strPageFileNameInput = "C:\pagefile.sys"             End If             WScript.Echo "New pagefile file will be: " & strPageFileNameInput             WScript.Echo "New pagefile size will be: " & strPageFileSizeInput & "MB"             bNoStaticPageFile = False                         '// Change the registry to add paging file information:             objShellApp.Exec("reg add ""HKLM\System\CurrentControlSet\Control\Session Manager\Memory Management"" /v PagingFiles /t REG_MULTI_SZ /d """ & strPageFileNameInput & " " & strPageFileSizeInput & " " & strPageFileSizeInput & """ /f")                                 '// Check the return to make sure we succeeded - otherwise, tell the user why we didn't:                 If Not Err.Number = 0 Then                     gErrData = gErrData & vbCrLf & "Error writing to registry - reason: " & Err.Number & "  - " & Err.Description                     WScript.Echo gErrData                     Err.Clear                 Else                     WScript.Echo "Registry edit to add " & strPageFileNameInput & " as size " & strPageFileSizeInput & "MB was successful."                     bRebootNeeded = True                 End If                         '// If we've made changes that have succeeded, we need to reboot - otherwise, do nothing:             If bRebootNeeded = True Then                 RebootSystem()                 WScript.Quit             Else                 WScript.Echo "No changes made that require a reboot.  Exiting script."                 WScript.Quit             End If                 '// If the user left the input blank, delete the paging file - note that Delete and DeleteEx from Win32_PagingFile are deprecated, and         '// while they'll return 0 on Windows 7, they don't actually delete the paging file.  Using reg add instead, as it works just fine:         Else             WScript.Echo "Deleting paging file: " & objDynamicPageFileItem.Name             objShellApp.Exec("reg add ""HKLM\System\CurrentControlSet\Control\Session Manager\Memory Management"" /v PagingFiles /t REG_MULTI_SZ /d """" /f")                         '// Check the return to make sure we succeeded - otherwise, tell the user why we didn't:             If Not Err.Number = 0 Then                 gErrData = gErrData & vbCrLf & "Error writing to registry - reason: " & Err.Number & "  - " & Err.Description                 WScript.Echo gErrData                 Err.Clear             Else                 WScript.Echo "Registry edit to remove " & objDynamicPageFileItem.Name & " was successful."                 bRebootNeeded = True             End If         End If     Next End Sub Sub RebootSystem()     WScript.Echo "The system will reboot in 5 seconds..."     Set isRunning = objShellApp.Exec("shutdown /r /t 5 /f /d p:2:4")     '// As long as shutdown.exe is running, wait:     Do While isRunning.Status = 0         WScript.Sleep 100         execOutput = isRunning.StdOut.ReadAll     Loop End Sub Function UserInput(strInput)     WScript.StdOut.Write strInput & " "     UserInput = WScript.StdIn.ReadLine End Function On Error GoTo 0 Sub RunMeWithCScript()     Dim ScriptEngine, engineFolder, Args, arg, scriptName, argString, scriptCommand         ScriptEngine = UCase(Mid(WScript.FullName, InstrRev(WScript.FullName, "\") + 1))     engineFolder = Left(WScript.FullName, InstrRev(WScript.FullName, "\"))     argString = ""         If ScriptEngine = "WSCRIPT.EXE" Then         Dim Shell         Set Shell = CreateObject("WScript.Shell")         Set Args = WScript.Arguments                 For Each arg in Args             If InStr(arg, " ") > 0 Then arg = """" & arg & """"             argString = argString & " " & Arg         Next                 scriptCommand = "cmd.exe /k " & engineFolder & "cscript.exe """ & WScript.ScriptFullName & """" & argString                 Shell.Run scriptCommand, , False         WScript.Quit     Else         Exit Sub     End If End Sub

Cluberti.com

You may also like...

2 Responses

  1. LastOS says:

    Microsoft didn’t remove the Old System Properties that had the tab with Set Page file, they simply don’t add a link to it anywhere in Windows 7 – all the registry does is puts the option (link) back on Computer icons context menu for the old System Properties.

  2. cluberti says:

    Yes, and running a script and entering a few numbers is much quicker than running a command, clicking the Advanced tab, clicking the Settings button under Performance, clicking another Advanced tab, clicking the Change button, unchecking “Automatically manage paging file size for all drives”, clicking the “Custom size:” radio button, entering in the same numbers, then clicking the Set button, the OK button, and then clicking another button to reboot the box when prompted.

    Yes, that is definitely preferable on thousands of machines to typing in one number in a script.

Leave a Reply

Your email address will not be published. Required fields are marked *

six + 7 =

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