Use WMI to remove a page file

8,106

Solution 1

Found it.

There is a .Delete() method that does the trick.

PS D:\> $pf[1].Delete()
PS D:\> gwmi win32_pagefilesetting

                            MaximumSize Name                                    Caption
                            ----------- ----                                    -------
                                   4096 c:\pagefile.sys                         c:\ 'pagefile.sys'

Done.

Solution 2

Although not recommended by some, if you would like to disable pagefiles completely, be sure to disable Automatic page management as well:

# Disable automatic pagefile management
$cs = gwmi Win32_ComputerSystem
if ($cs.AutomaticManagedPagefile) {
    $cs.AutomaticManagedPagefile = $False
    $cs.Put()
}
# Disable a *single* pagefile if any
$pg = gwmi win32_pagefilesetting
if ($pg) {
    $pg.Delete()
}
Share:
8,106

Related videos on Youtube

Andrew J. Brehm
Author by

Andrew J. Brehm

Not a penguin and not a Linux user either.

Updated on September 18, 2022

Comments

  • Andrew J. Brehm
    Andrew J. Brehm over 1 year

    I can modify page file settings via WMI like this

    PS D:\> gwmi win32_pagefilesetting
    
                                MaximumSize Name                                    Caption
                                ----------- ----                                    -------
                                       8192 c:\pagefile.sys                         c:\ 'pagefile.sys'
                                       8192 d:\pagefile.sys                         d:\ 'pagefile.sys'
    
    
    PS D:\> $pf=gwmi win32_pagefilesetting
    PS D:\> $pf.gettype()
    
    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     Object[]                                 System.Array
    
    PS D:\> $pf[0].InitialSize=4096;$pf[0].MaximumSize=4096
    PS D:\> $pf[0].Put()
    PS D:\> gwmi win32_pagefilesetting
    
                                MaximumSize Name                                    Caption
                                ----------- ----                                    -------
                                       4096 c:\pagefile.sys                         c:\ 'pagefile.sys'
                                       8192 d:\pagefile.sys                         d:\ 'pagefile.sys'
    

    But how can I remove a page file setting, e.g. in this remove the page file on D:?