PowerShell Function Set-PageFile

Here is a function for working with the Windows page file. You can use it to change the path (it will delete the old one and make a new one for you) or modify initial and maximum size.

function Set-PageFile {
        Sets Page File to custom size
        Disables automatic management of the pagefile, then applies the given values for path and page file size.
        Defaults to C:\pagefile.sys with a 4 gig pagefile.
        The page file's fully qualified file name (such as C:\pagefile.sys)
    .PARAMETER InitialSize
        The page file's initial size [MB]
    .PARAMETER MaximumSize
        The page file's maximum size [MB]
        C:\PS> Set-PageFile "C:\pagefile.sys" 4096 6144
        [string]$Path = "C:\pagefile.sys",
        [int]$InitialSize = 4096,
        [int]$MaximumSize = 4096
    $ComputerSystem = $null
    $CurrentPageFile = $null
    $modify = $false
    # Disables automatically managed page file setting first
    $ComputerSystem = Get-WmiObject -Class Win32_ComputerSystem -EnableAllPrivileges
    if ($ComputerSystem.AutomaticManagedPagefile) {
        $ComputerSystem.AutomaticManagedPagefile = $false
    $CurrentPageFile = Get-WmiObject -Class Win32_PageFileSetting
    if ($CurrentPageFile.Name -eq $Path) {
        # Keeps the existing page file
        if ($CurrentPageFile.InitialSize -ne $InitialSize) {
            $CurrentPageFile.InitialSize = $InitialSize
            $modify = $true
        if ($CurrentPageFile.MaximumSize -ne $MaximumSize) {
            $CurrentPageFile.MaximumSize = $MaximumSize
            $modify = $true
        if ($modify) { $CurrentPageFile.Put() }
    else {
        # Creates a new page file
        Set-WmiInstance -Class Win32_PageFileSetting -Arguments @{Name=$Path; InitialSize = $InitialSize; MaximumSize = $MaximumSize}
This entry was posted in Enterprise Management, Programming and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s