Use WMI to remove a page file

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.


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()
}