'PowerShell Script to set the size of pagefile.sys

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



Solution 1:[1]

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;

Solution 2:[2]

Well, to GET the pagefile with Powershell use this function I got from Mike Kanakos: "function-getpagefilessize.ps1", which, for some reason, does not work from the profile as a PS file or PSM file:

Function Get-PageFileInfo {

<# 
 .Synopsis 
  Returns info about the page file size of a Windows computer. Defaults to local machine. 

 .Description 
  Returns the pagefile size info in MB. Also returns the PageFilePath, PageFileTotalSize, PagefileCurrentUsage,
  and PageFilePeakusage. Also returns if computer is using a TempPafeFile and if the machine's pagefile is
  managed by O/S (AutoManaged = true) or statically set (AutoManaged = False)

  

 .Example 
  Get-PageFileInfo -computername SRV01
  Returns pagefile info for the computer named SRV01

  Computer             : SRV01
  FilePath             : C:\pagefile.sys
  AutoManagedPageFile  : True
  TotalSize (in MB)    : 8192
  CurrentUsage (in MB) : 60
  PeakUsage (in MB)    : 203
  TempPageFileInUse    : False


 .Example 
  Get-PageFileInfo SRV01, SRV02
  Returns pagefile info for two computers named SRV01 & DC02.

  Computer             : SRV01
  FilePath             : C:\pagefile.sys
  AutoManagedPageFile  : True
  TotalSize (in MB)    : 8192
  CurrentUsage (in MB) : 60
  PeakUsage (in MB)    : 203
  TempPageFileInUse    : False

  Computer             : SRV02
  FilePath             : C:\pagefile.sys
  AutoManagedPageFile  : True
  TotalSize (in MB)    : 8192
  CurrentUsage (in MB) : 0
  PeakUsage (in MB)    : 0
  TempPageFileInUse    : False

.Example 
  Get-PageFileInfo SRV01, SRV02, SRV03 | Format-Table
  Returns pagefile info for three computers named SRV01, SRV02 & SRV03 in a table format.


  Computer  FilePath        AutoManagedPageFile TotalSize (in MB) CurrentUsage (in MB) PeakUsage (in MB) TempPageFileInUse
  --------  --------        ------------------- ----------------- -------------------- ----------------- -----------------
  SRV01    C:\pagefile.sys                True              8192                   60               203             False
  SRV02    C:\pagefile.sys                True             13312                    0                 0             False
  SRV03    C:\pagefile.sys                True              2432                    0                 0             False

 
  .Parameter computername 
  The name of the computer to query. Required field.

 .Notes 
  NAME: Get-PageFileInfo 
  AUTHOR: Mike Kanakos 
  Version: v1.1
  LASTEDIT: Thursday, August 30, 2018 2:19:18 PM
  
  .Link 
  
  
#> 

[CmdletBinding()]
Param(
    [Parameter(Mandatory=$True,ValueFromPipeline=$True)]  
    [string[]]$ComputerName
)

# Main Part of function


Foreach ($computer in $ComputerName)
{

  $online= Test-Connection -ComputerName $computer -Count 2 -Quiet
    if ($online -eq $true)
     {
      $PageFileResults = Get-CimInstance -Class Win32_PageFileUsage -ComputerName $Computer | Select-Object *
      $CompSysResults = Get-CimInstance win32_computersystem -ComputerName $computer -Namespace 'root\cimv2'
    
      $PageFileStats = [PSCustomObject]@{
        Computer = $computer
        FilePath = $PageFileResults.Description
        AutoManagedPageFile = $CompSysResults.AutomaticManagedPagefile
        "TotalSize(in MB)" = $PageFileResults.AllocatedBaseSize
        "CurrentUsage(in MB)"  = $PageFileResults.CurrentUsage
        "PeakUsage(in MB)" = $PageFileResults.PeakUsage
        TempPageFileInUse = $PageFileResults.TempPageFile
      } #END PSCUSTOMOBJECT
     } #END IF
    else
     {
        # Computer is not reachable!
        Write-Host "Error: $computer not online" -Foreground white -BackgroundColor Red
     } # END ELSE


  $PageFileStats
 
} #END FOREACH


} #END FUNCTION

#AUTHOR: Mike Kanakos#

But setting the page file then runs into ALL kinds of issues. Like, it gets REAL buggy. IF it is set you can change it, if it is not, you must first set to system managed and then set to something, like 16384/16384 on C and System managed on D:. I am working on the answer myself, because I need this, and I will get back to ya'll when I get it sorted (amongst my long list of other scripts to do)... But, ITMT, the function will help. Do a ForEach to a list like this:

remove-item -Force volumeletter:\folder\outputfile.txt
$results = (Get-PageFileInfo -Verbose server1.domain,server2.domain |select * |format-table -AutoSize)
$results |out-file volumeletter:\folder\outputfile.txt -Force ascii

OR you load up the Function in ISE, run it to load into memory and then query each server manually from the PS cmd:

Get-PageFileInfo server1.domain

Computer            : server1.domain
FilePath            : {C:\pagefile.sys, D:\pagefile.sys}
AutoManagedPageFile : False
TotalSize(in MB)    : {16384, 768}
CurrentUsage(in MB) : {88, 62}
PeakUsage(in MB)    : {120, 84}
TempPageFileInUse   : {False, False}

You will see blanks if you are required to use FQDN... IF you have the standard system managed the function will give you the usage, and that will tell you if you have to set the static sizes:

Get-PageFileInfo server2.domain

Computer            : server2.domain
FilePath            : C:\pagefile.sys
AutoManagedPageFile : True
TotalSize(in MB)    : 7679
CurrentUsage(in MB) : 1763
PeakUsage(in MB)    : 4867
TempPageFileInUse   : False

Solution 3:[3]

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"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pratik Patil
Solution 2
Solution 3 Patrick Burwell