How to change Pagefile settings using PowerShell?

PowerShellMicrosoft TechnologiesSoftware & Coding

To change the pagefile settings, we will divide this into multiple sections. First, when Pagefile is automatically managed, we can’t modify the settings so we need to remove that box. In GUI that box can be unchecked in Virtual memory settings.

Code to uncheck the above box.

$pagefile = Get-WmiObject Win32_ComputerSystem -EnableAllPrivileges
$pagefile.AutomaticManagedPagefile = $false
$pagefile.put() | Out-Null

So once the above code is executed, other fields will be enabled. We now need to Customize the size of the pagefile in the below image by providing Initial and Maximum size using PowerShell.

Here we have already the pagefile on the C: while on the E: there is no pagefile set. First, we will set the pagefile on C Drive.

$pagefileset = Get-WmiObject Win32_pagefilesetting
$pagefileset.InitialSize = 1024
$pagefileset.MaximumSize = 2048
$pagefileset.Put() | Out-Null

Once you run the above command and check the pagefile settings, it will be customized on C Drive. You may need to reboot the system after setting the pagefile.

PS C:\> Gwmi win32_Pagefilesetting | Select Name, InitialSize, MaximumSize
Name             InitialSize   MaximumSize
----             -----------   -----------
C:\pagefile.sys  1024          2048

If you have another scenario, like setting up pagefile on the different drive for example in the above image we have E: as well. If there is no pagefile set on the drive then we need to set WMI instance for it.

Set-WmiInstance -Class Win32_PageFileSetting -Arguments @{name="E:\pagefile.sys";
InitialSize = 0; MaximumSize = 0} -EnableAllPrivileges | Out-Null

We have now set the pagefile on E: and it is system managed.

PS C:\> Gwmi win32_pagefilesetting | where{$_.caption -like 'E:*'}
MaximumSize        Name     Caption
-----------        ----     -------
0 E:\pagefile.sys  E:\ 'pagefile.sys'

If there are multiple pagefiles on the system and if we need to customize the size to the specific drive, we need to filter out the drive. In this case, we need to customize on E: so, we can filter this pagefile and modify settings.

$pagefileset = Gwmi win32_pagefilesetting | where{$_.caption -like 'E:*'}
$pagefileset.InitialSize = 1024
$pagefileset.MaximumSize = 2048
$pagefileset.Put() | Out-Null

The size of the pagefile is now customized on E:

raja
Published on 05-Oct-2020 07:09:29
Advertisements