Site icon WinCert

PageFile vbs for Vista and Win7 systems

<p><a href&equals;"http&colon;&sol;&sol;wincert&period;net&sol;wp-content&sol;uploads&sol;2015&sol;01&sol;windows-7-2&period;jpg"><img class&equals;"alignnone size-full wp-image-547" src&equals;"http&colon;&sol;&sol;wincert&period;net&sol;wp-content&sol;uploads&sol;2015&sol;01&sol;windows-7-2&period;jpg" alt&equals;"Windows 7 Box&comma;domain logon&comma;admin pack&comma;taskbar thumbnails&comma;task scheduler&comma;preview pane&comma;windows 7&comma;chrome theme&comma;icon cache&comma;admin shares&comma;install the printer&comma;drives" width&equals;"720" height&equals;"340" &sol;><&sol;a><&sol;p>&NewLine;<p>A good friend of mine wrote up a very small vbscript to handle paging file creation&sol;deletion&sol;modification on Vista&sol;Win7 systems &lpar;as PageFileConfig&period;vbs doesn’t exist inbox&comma; and it didn’t have the ability to handle some of the situations that can arise during setup&rpar;&comma; and it ended up morphing into this&period;<&sol;p>&NewLine;<p>Script might work on Windows XP&sol;2003 system&comma; but it&&num;8217&semi;s not tested yet&period; <&excl;--more--><&sol;p>&NewLine;<p>He thinks that it would save someone the time if they had a need for it&comma; so here it is&colon;<&sol;p>&NewLine;<pre>'&sol;&sol; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ '&sol;&sol; '&sol;&sol; NAME&colon;        setpagefile&period;vbs '&sol;&sol; '&sol;&sol; Original&colon;    http&colon;&sol;&sol;www&period;cluberti&period;com&sol;blog '&sol;&sol; Last Update&colon; 16th June 2010 '&sol;&sol; '&sol;&sol; Comment&colon;     VBS example file for use in configuring the paging file on Windows '&sol;&sol; '&sol;&sol; NOTE&colon;        Provided as-is - usage of this source assumes that you are at the '&sol;&sol;              very least familiar with the vbscript language being used and '&sol;&sol;              the tools used to create and debug this file&period; '&sol;&sol; '&sol;&sol;              In other words&comma; if you break it&comma; you get to keep the pieces&period; '&sol;&sol; '&sol;&sol; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Option Explicit Dim strComputer&comma; strPageFileSizeInput&comma; strPageFileNameInput&comma; strInput Dim objWMIService&comma; objStaticPageFileItem&comma; objDynamicPageFileItem&comma; objShellApp Dim colStaticPageFileItems&comma; colDynamicPageFileItems&comma; isRunning&comma; execOutput&comma; gErrData&comma; bRebootNeeded&comma; bNoStaticPageFile&comma; bNoPageFile '&sol;&sol; Ensure that cscript is the engine used to run this script&colon; RunMeWithCScript&lpar;&rpar; On Error Resume Next '&sol;&sol; Configure our variables&colon; strComputer &equals; "&period;" bRebootNeeded &equals; False bNoStaticPageFile &equals; True bNoPageFile &equals; True Set objShellApp &equals; CreateObject&lpar;"WScript&period;Shell"&rpar; Set objWMIService &equals; GetObject&lpar;"winmgmts&colon;&bsol;&bsol;" &amp&semi; strComputer &amp&semi; "&bsol;root&bsol;CIMV2"&rpar; '&sol;&sol; Win32&lowbar;PageFile is deprecated - it still &lpar;mostly&rpar; works&comma; but Win32&lowbar;PageFileSetting is not deprecated&comma; so use that&colon; Set colStaticPageFileItems &equals; objWMIService&period;ExecQuery&lpar;"SELECT &ast; FROM Win32&lowbar;PageFileSetting"&comma;&comma;48&rpar; Set colDynamicPageFileItems &equals; objWMIService&period;ExecQuery&lpar;"SELECT &ast; FROM Win32&lowbar;PageFileUsage"&comma;&comma;48&rpar; '&sol;&sol; Execute the ChangePagingFile sub&colon; ChangePagingFile&lpar;&rpar; '&sol;&sol; Check execution of ChangePagingFile&lpar;&rpar;&comma; and handle when "system managed" is set&comma; or no paging file exists&colon; If bRebootNeeded &equals; True Then     RebootSystem&lpar;&rpar; Else     '&sol;&sol; Can't use WMI to create a paging file when it is "system managed" or none exists - handle this condition&colon;     If bNoStaticPageFile &equals; True Then         CheckForDynamicPagingFile&lpar;&rpar;         If bNoPageFile &equals; False Then             WScript&period;Quit         Else             If bNoPageFile &equals; True Then                 WScript&period;Echo "No paging file found&period;"                 WScript&period;Echo ""                 strPageFileNameInput &equals; UserInput&lpar;"Enter new pagefile name &lpar;full path and filename&rpar; - leave blank to use C&colon;&bsol;pagefile&period;sys&colon; "&rpar;                 strPageFileSizeInput &equals; UserInput&lpar;"Enter new pagefile size &lpar;in MB&rpar; - leave blank to keep the system with no paging file&colon;  "&rpar;                                 '&sol;&sol; Check to see if the user left the input blank&comma; otherwise set up a paging file&colon;                 If Not strPageFileSizeInput &equals; "" Then                     If strPageFileNameInput &equals; "" Then                         strPageFileNameInput &equals; "C&colon;&bsol;pagefile&period;sys"                     End If                     WScript&period;Echo "New pagefile file will be&colon; " &amp&semi; strPageFileNameInput                     WScript&period;Echo "New pagefile size will be&colon; " &amp&semi; strPageFileSizeInput &amp&semi; "MB"                     bNoStaticPageFile &equals; False                                         '&sol;&sol; Change the registry to add paging file information&colon;                     objShellApp&period;Exec&lpar;"reg add ""HKLM&bsol;System&bsol;CurrentControlSet&bsol;Control&bsol;Session Manager&bsol;Memory Management"" &sol;v PagingFiles &sol;t REG&lowbar;MULTI&lowbar;SZ &sol;d """ &amp&semi; strPageFileNameInput &amp&semi; " " &amp&semi; strPageFileSizeInput &amp&semi; " " &amp&semi; strPageFileSizeInput &amp&semi; """ &sol;f"&rpar;                                         '&sol;&sol; Check the return to make sure we succeeded - otherwise&comma; tell the user why we didn't&colon;                     If Not Err&period;Number &equals; 0 Then                         gErrData &equals; gErrData &amp&semi; vbCrLf &amp&semi; "Error writing to registry - reason&colon; " &amp&semi; Err&period;Number &amp&semi; "  - " &amp&semi; Err&period;Description                         WScript&period;Echo gErrData                         Err&period;Clear                     Else                         WScript&period;Echo "Registry edit to add " &amp&semi; strPageFileNameInput &amp&semi; " as size " &amp&semi; strPageFileSizeInput &amp&semi; "MB was successful&period;"                         bRebootNeeded &equals; True                     End If                                 '&sol;&sol; If we've made changes that have succeeded&comma; we need to reboot - otherwise&comma; do nothing&colon;                     If bRebootNeeded &equals; True Then                         RebootSystem&lpar;&rpar;                     Else                         WScript&period;Echo "No changes made that require a reboot&period;  Exiting script&period;"                     End If                 Else                     WScript&period;Echo "You have elected to continue with no paging file&period;  Exiting script&period;"                 End If             Else                 WScript&period;Echo "No changes made that require a reboot&period;  Exiting script&period;"             End If         End If     End If End If Sub ChangePagingFile&lpar;&rpar;     For Each objStaticPageFileItem In colStaticPageFileItems         '&sol;&sol; There's a static paging file - use WMI to make changes&colon;         bNoStaticPageFile &equals; False         bNoPageFile &equals; False                 WScript&period;Echo " --------------------------"         WScript&period;Echo "  Current PageFile Details"         WScript&period;Echo " --------------------------"         WScript&period;Echo "Name&colon;                " &amp&semi; objStaticPageFileItem&period;Name         WScript&period;Echo "InitialSize&colon;         " &amp&semi; objStaticPageFileItem&period;InitialSize         WScript&period;Echo "MaximumSize&colon;         " &amp&semi; objStaticPageFileItem&period;MaximumSize         WScript&period;Echo ""                 '&sol;&sol; Get user input&colon;         strInput &equals; UserInput&lpar;"Enter new pagefile size &lpar;in MB&rpar; - leave blank to delete existing paging file&colon; "&rpar;                 '&sol;&sol; If the user entered a value&comma; configure the new paging file size&colon;         If Not strInput &equals; "" Then         WScript&period;Echo "New pagefile size will be&colon; " &amp&semi; strInput &amp&semi; "MB"             WScript&period;Echo ""                         objStaticPageFileItem&period;InitialSize &equals; strInput             objStaticPageFileItem&period;MaximumSize &equals; strInput             objStaticPageFileItem&period;Put&lowbar;             bRebootNeeded &equals; True                         WScript&period;Echo " --------------------------"             WScript&period;Echo "    New PageFile Details"             WScript&period;Echo " --------------------------"             WScript&period;Echo "Name&colon;                " &amp&semi; objStaticPageFileItem&period;Name             WScript&period;Echo "InitialSize&colon;         " &amp&semi; objStaticPageFileItem&period;InitialSize             WScript&period;Echo "MaximumSize&colon;         " &amp&semi; objStaticPageFileItem&period;MaximumSize             WScript&period;Echo ""             WScript&period;Echo "A reboot is needed for this change to take effect&period;"                 '&sol;&sol; If the user left the input blank&comma; delete the paging file - note that Delete and DeleteEx from Win32&lowbar;PagingFile are deprecated&comma; and         '&sol;&sol; while they'll return 0 on Windows 7&comma; they don't actually delete the paging file&period;  Using reg add instead&comma; as it works just fine&colon;         Else             WScript&period;Echo "Deleting paging file&colon; " &amp&semi; objStaticPageFileItem&period;Name             objShellApp&period;Exec&lpar;"reg add ""HKLM&bsol;System&bsol;CurrentControlSet&bsol;Control&bsol;Session Manager&bsol;Memory Management"" &sol;v PagingFiles &sol;t REG&lowbar;MULTI&lowbar;SZ &sol;d """" &sol;f"&rpar;                         '&sol;&sol; Check the return to make sure we succeeded - otherwise&comma; tell the user why we didn't&colon;             If Not Err&period;Number &equals; 0 Then                 gErrData &equals; gErrData &amp&semi; vbCrLf &amp&semi; "Error writing to registry - reason&colon; " &amp&semi; Err&period;Number &amp&semi; "  - " &amp&semi; Err&period;Description                 WScript&period;Echo gErrData                 Err&period;Clear             Else                 WScript&period;Echo "Registry edit to remove " &amp&semi; objStaticPageFileItem&period;Name &amp&semi; " was successful&period;"                 bRebootNeeded &equals; True             End If         End If     Next End Sub Sub CheckForDynamicPagingFile&lpar;&rpar;     For Each objDynamicPageFileItem In colDynamicPageFileItems         '&sol;&sol; There's a paging file - use WMI to make changes&colon;         bNoPageFile &equals; False                 WScript&period;Echo "Paging file is configured for 'System managed size'"         WScript&period;Echo ""         WScript&period;Echo " --------------------------"         WScript&period;Echo "  Current PageFile Details"         WScript&period;Echo " --------------------------"         WScript&period;Echo "Name&colon;                " &amp&semi; objDynamicPageFileItem&period;Name         WScript&period;Echo "Size&colon;                " &amp&semi; objDynamicPageFileItem&period;AllocatedBaseSize         WScript&period;Echo "Current Usage&colon;       " &amp&semi; objDynamicPageFileItem&period;CurrentUsage         WScript&period;Echo ""                 WScript&period;Echo ""         strPageFileNameInput &equals; UserInput&lpar;"Enter new pagefile name &lpar;full path and filename&rpar; - leave blank to use C&colon;&bsol;pagefile&period;sys&colon; "&rpar;         strPageFileSizeInput &equals; UserInput&lpar;"Enter new pagefile size &lpar;in MB&rpar; - leave blank to remove the paging file&colon;               "&rpar;                 '&sol;&sol; Check to see if the user left the input blank&comma; otherwise set up a paging file&colon;         If Not strPageFileSizeInput &equals; "" Then             If strPageFileNameInput &equals; "" Then                 strPageFileNameInput &equals; "C&colon;&bsol;pagefile&period;sys"             End If             WScript&period;Echo "New pagefile file will be&colon; " &amp&semi; strPageFileNameInput             WScript&period;Echo "New pagefile size will be&colon; " &amp&semi; strPageFileSizeInput &amp&semi; "MB"             bNoStaticPageFile &equals; False                         '&sol;&sol; Change the registry to add paging file information&colon;             objShellApp&period;Exec&lpar;"reg add ""HKLM&bsol;System&bsol;CurrentControlSet&bsol;Control&bsol;Session Manager&bsol;Memory Management"" &sol;v PagingFiles &sol;t REG&lowbar;MULTI&lowbar;SZ &sol;d """ &amp&semi; strPageFileNameInput &amp&semi; " " &amp&semi; strPageFileSizeInput &amp&semi; " " &amp&semi; strPageFileSizeInput &amp&semi; """ &sol;f"&rpar;                                 '&sol;&sol; Check the return to make sure we succeeded - otherwise&comma; tell the user why we didn't&colon;                 If Not Err&period;Number &equals; 0 Then                     gErrData &equals; gErrData &amp&semi; vbCrLf &amp&semi; "Error writing to registry - reason&colon; " &amp&semi; Err&period;Number &amp&semi; "  - " &amp&semi; Err&period;Description                     WScript&period;Echo gErrData                     Err&period;Clear                 Else                     WScript&period;Echo "Registry edit to add " &amp&semi; strPageFileNameInput &amp&semi; " as size " &amp&semi; strPageFileSizeInput &amp&semi; "MB was successful&period;"                     bRebootNeeded &equals; True                 End If                         '&sol;&sol; If we've made changes that have succeeded&comma; we need to reboot - otherwise&comma; do nothing&colon;             If bRebootNeeded &equals; True Then                 RebootSystem&lpar;&rpar;                 WScript&period;Quit             Else                 WScript&period;Echo "No changes made that require a reboot&period;  Exiting script&period;"                 WScript&period;Quit             End If                 '&sol;&sol; If the user left the input blank&comma; delete the paging file - note that Delete and DeleteEx from Win32&lowbar;PagingFile are deprecated&comma; and         '&sol;&sol; while they'll return 0 on Windows 7&comma; they don't actually delete the paging file&period;  Using reg add instead&comma; as it works just fine&colon;         Else             WScript&period;Echo "Deleting paging file&colon; " &amp&semi; objDynamicPageFileItem&period;Name             objShellApp&period;Exec&lpar;"reg add ""HKLM&bsol;System&bsol;CurrentControlSet&bsol;Control&bsol;Session Manager&bsol;Memory Management"" &sol;v PagingFiles &sol;t REG&lowbar;MULTI&lowbar;SZ &sol;d """" &sol;f"&rpar;                         '&sol;&sol; Check the return to make sure we succeeded - otherwise&comma; tell the user why we didn't&colon;             If Not Err&period;Number &equals; 0 Then                 gErrData &equals; gErrData &amp&semi; vbCrLf &amp&semi; "Error writing to registry - reason&colon; " &amp&semi; Err&period;Number &amp&semi; "  - " &amp&semi; Err&period;Description                 WScript&period;Echo gErrData                 Err&period;Clear             Else                 WScript&period;Echo "Registry edit to remove " &amp&semi; objDynamicPageFileItem&period;Name &amp&semi; " was successful&period;"                 bRebootNeeded &equals; True             End If         End If     Next End Sub Sub RebootSystem&lpar;&rpar;     WScript&period;Echo "The system will reboot in 5 seconds&period;&period;&period;"     Set isRunning &equals; objShellApp&period;Exec&lpar;"shutdown &sol;r &sol;t 5 &sol;f &sol;d p&colon;2&colon;4"&rpar;     '&sol;&sol; As long as shutdown&period;exe is running&comma; wait&colon;     Do While isRunning&period;Status &equals; 0         WScript&period;Sleep 100         execOutput &equals; isRunning&period;StdOut&period;ReadAll     Loop End Sub Function UserInput&lpar;strInput&rpar;     WScript&period;StdOut&period;Write strInput &amp&semi; " "     UserInput &equals; WScript&period;StdIn&period;ReadLine End Function On Error GoTo 0 Sub RunMeWithCScript&lpar;&rpar;     Dim ScriptEngine&comma; engineFolder&comma; Args&comma; arg&comma; scriptName&comma; argString&comma; scriptCommand         ScriptEngine &equals; UCase&lpar;Mid&lpar;WScript&period;FullName&comma; InstrRev&lpar;WScript&period;FullName&comma; "&bsol;"&rpar; &plus; 1&rpar;&rpar;     engineFolder &equals; Left&lpar;WScript&period;FullName&comma; InstrRev&lpar;WScript&period;FullName&comma; "&bsol;"&rpar;&rpar;     argString &equals; ""         If ScriptEngine &equals; "WSCRIPT&period;EXE" Then         Dim Shell         Set Shell &equals; CreateObject&lpar;"WScript&period;Shell"&rpar;         Set Args &equals; WScript&period;Arguments                 For Each arg in Args             If InStr&lpar;arg&comma; " "&rpar; &gt&semi; 0 Then arg &equals; """" &amp&semi; arg &amp&semi; """"             argString &equals; argString &amp&semi; " " &amp&semi; Arg         Next                 scriptCommand &equals; "cmd&period;exe &sol;k " &amp&semi; engineFolder &amp&semi; "cscript&period;exe """ &amp&semi; WScript&period;ScriptFullName &amp&semi; """" &amp&semi; argString                 Shell&period;Run scriptCommand&comma; &comma; False         WScript&period;Quit     Else         Exit Sub     End If End Sub<&sol;pre>&NewLine;<p><a href&equals;"http&colon;&sol;&sol;www&period;cluberti&period;com&sol;blog&sol;2010&sol;06&sol;17&sol;pagefile-vbs-for-vistawin7-systems-may-work-on-xp2003-not-tested&sol;" target&equals;"&lowbar;blank">Cluberti&period;com<&sol;a><&sol;p>&NewLine;

Exit mobile version