How to set the size of Page File on Windows(pagefile.sys) via PowerShell?
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.
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.
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;
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With