Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell Script to set the size of pagefile.sys

How to set the size of Page File on Windows(pagefile.sys) via PowerShell?

like image 406
Jayu Avatar asked Jun 14 '16 13:06

Jayu


People also ask

How do I Auto manage pagefile size?

Open the virtual memory settings and under the Change option check the "Automatically manage paging file size for all drives" or "System managed size" depending on your version of Windows. Reboot the computer after changing the settings.

How do I set pagefile sys?

Set a Specific Pagefile AmountGo to the Start Menu and click on Settings. Type performance. Choose Adjust the appearance and performance of Windows. In the new window, go to the Advanced tab and under the Virtual memory section, click on Change.


2 Answers

This is how we can update the size of pagefile.sys via PowerShell:

# PowerShell Script to set the size of pagefile.sys

$computersys = Get-WmiObject Win32_ComputerSystem -EnableAllPrivileges;
$computersys.AutomaticManagedPagefile = $False;
$computersys.Put();
$pagefile = Get-WmiObject -Query "Select * From Win32_PageFileSetting Where Name like '%pagefile.sys'";
$pagefile.InitialSize = <New_Value_For_Size_In_MB>;
$pagefile.MaximumSize = <New_Value_For_Size_In_MB>;
$pagefile.Put();

Execute the script as below:

PS> .\update_pagefile_size.ps1;
like image 55
Pratik Patil Avatar answered Oct 23 '22 23:10

Pratik Patil


HERE is the solution THIS is to set C VOL page file to 16384MB, static and D VOL page file to System managed:

# PowerShell Script to set the size of pagefile.sys
# update_pagefile_size.ps1
$pagefile = Get-WmiObject Win32_ComputerSystem -EnableAllPrivileges
$pagefile.AutomaticManagedPagefile = $false
#$pagefile.put() | Out-Null
Gwmi win32_pagefilesetting | where{$_.caption -like 'C:*'}
$pagefileset = Gwmi win32_pagefilesetting | where{$_.caption -like 'C:*'}
$pagefileset.InitialSize = 16384
$pagefileset.MaximumSize = 16384
$pagefileset.Put() | Out-Null

if((Set-WmiInstance -Class Win32_PageFileSetting -Arguments @{name="C:\pagefile.sys";InitialSize = $pagefileset.InitialSize; MaximumSize = $pagefileset.MaximumSize} -EnableAllPrivileges -Verbose) -icontains "already exists"){
$pagefileset = Gwmi win32_pagefilesetting | where{$_.caption -like 'C:*'}
$pagefileset.Delete()
$pagefileset.InitialSize = 16384
$pagefileset.MaximumSize = 16384
Set-WmiInstance -Class Win32_PageFileSetting -Arguments @{name="C:\pagefile.sys";InitialSize = $pagefileset.InitialSize; MaximumSize = $pagefileset.MaximumSize} -EnableAllPrivileges
Gwmi win32_pagefilesetting | where{$_.caption -like 'C:*'}
}
$pagefileset = Gwmi win32_pagefilesetting | where{$_.caption -like 'D:*'}
$pagefileset.InitialSize = 0
$pagefileset.MaximumSize = 0
$pagefileset.Put() | Out-Null

Gwmi win32_pagefilesetting | where{$_.caption -like 'D:*'}
Set-WmiInstance -Class Win32_PageFileSetting -Arguments @{name="D:\pagefile.sys";InitialSize = 0; MaximumSize = 0} -EnableAllPrivileges | Out-Null

Write-host "Don't forget to reboot"
#shutdown /r /t 120 /c "rebooting to fix pagefile"
like image 1
Patrick Burwell Avatar answered Oct 23 '22 23:10

Patrick Burwell