Powershell script to change Pagefile (Virtual memory) size on remote computers using PDQ deploy.

Comments

12 comments

  • Stephen Valdinger

    Can you post what you are using? And any error information? I suspect you would want to use WMI to accomplish this.

    0
    Comment actions Permalink
  • Tim Moseley

    Here is the script from the PS that I dowloaded:

    #Requires -Version 3.0

    Function Set-PageFile {
    <#
    .SYNOPSIS
    Set-PageFile is an advanced function which can be used to adjust virtual memory page file size.
    .DESCRIPTION
    Set-PageFile is an advanced function which can be used to adjust virtual memory page file size.
    .PARAMETER <InitialSize>
    Setting the paging file's initial size.
    .PARAMETER <MaximumSize>
    Setting the paging file's maximum size.
    .PARAMETER <DriveLetter>
    Specifies the drive letter you want to configure.
    .PARAMETER <SystemManagedSize>
    Allow Windows to manage page files on this computer.
    .PARAMETER <None>
    Disable page files setting.
    .PARAMETER <Reboot>
    Reboot the computer so that configuration changes take effect.
    .PARAMETER <AutoConfigure>
    Automatically configure the initial size and maximumsize.
    .EXAMPLE
    C:\PS> Set-PageFile -InitialSize 1024 -MaximumSize 2048 -DriveLetter "C:","D:"

    Execution Results: Set page file size on "C:" successful.
    Execution Results: Set page file size on "D:" successful.

    Name InitialSize(MB) MaximumSize(MB)
    ---- --------------- ---------------
    C:\pagefile.sys 1024 2048
    D:\pagefile.sys 1024 2048
    E:\pagefile.sys 2048 2048
    .LINK
    Get-WmiObject
    http://technet.microsoft.com/library/hh849824.aspx
    #>
    [cmdletbinding(SupportsShouldProcess,DefaultParameterSetName="SetPageFileSize")]
    Param
    (
    [Parameter(Mandatory,ParameterSetName="SetPageFileSize")]
    [Alias('is')]
    [Int32]$InitialSize,

    [Parameter(Mandatory,ParameterSetName="SetPageFileSize")]
    [Alias('ms')]
    [Int32]$MaximumSize,

    [Parameter(Mandatory)]
    [Alias('dl')]
    [ValidatePattern('^[A-Z]$')]
    [String[]]$DriveLetter,

    [Parameter(Mandatory,ParameterSetName="None")]
    [Switch]$None,

    [Parameter(Mandatory,ParameterSetName="SystemManagedSize")]
    [Switch]$SystemManagedSize,

    [Parameter()]
    [Switch]$Reboot,

    [Parameter(Mandatory,ParameterSetName="AutoConfigure")]
    [Alias('auto')]
    [Switch]$AutoConfigure
    )
    Begin {}
    Process {
    If($PSCmdlet.ShouldProcess("Setting the virtual memory page file size")) {
    $DriveLetter | ForEach-Object -Process {
    $DL = $_
    $PageFile = $Vol = $null
    try {
    $Vol = Get-CimInstance -ClassName CIM_StorageVolume -Filter "Name='$($DL):\\'" -ErrorAction Stop
    } catch {
    Write-Warning -Message "Failed to find the DriveLetter $DL specified"
    return
    }
    if ($Vol.DriveType -ne 3) {
    Write-Warning -Message "The selected drive should be a fixed local volume"
    return
    }
    Switch ($PsCmdlet.ParameterSetName) {
    None {
    try {
    $PageFile = Get-CimInstance -Query "Select * From Win32_PageFileSetting Where Name='$($DL):\\pagefile.sys'" -ErrorAction Stop
    } catch {
    Write-Warning -Message "Failed to query the Win32_PageFileSetting class because $($_.Exception.Message)"
    }
    If($PageFile) {
    try {
    $PageFile | Remove-CimInstance -ErrorAction Stop
    } catch {
    Write-Warning -Message "Failed to delete pagefile the Win32_PageFileSetting class because $($_.Exception.Message)"
    }
    } Else {
    Write-Warning "$DL is already set None!"
    }
    break
    }
    SystemManagedSize {
    Set-PageFileSize -DL $DL -InitialSize 0 -MaximumSize 0
    break
    }
    AutoConfigure {
    $TotalPhysicalMemorySize = @()
    #Getting total physical memory size
    try {
    Get-CimInstance Win32_PhysicalMemory -ErrorAction Stop | ? DeviceLocator -ne "SYSTEM ROM" | ForEach-Object {
    $TotalPhysicalMemorySize += [Double]($_.Capacity)/1GB
    }
    } catch {
    Write-Warning -Message "Failed to query the Win32_PhysicalMemory class because $($_.Exception.Message)"
    }
    <#
    By default, the minimum size on a 32-bit (x86) system is 1.5 times the amount of physical RAM if physical RAM is less than 1 GB,
    and equal to the amount of physical RAM plus 300 MB if 1 GB or more is installed. The default maximum size is three times the amount of RAM,
    regardless of how much physical RAM is installed.
    If($TotalPhysicalMemorySize -lt 1) {
    $InitialSize = 1.5*1024
    $MaximumSize = 1024*3
    Set-PageFileSize -DL $DL -InitialSize $InitialSize -MaximumSize $MaximumSize
    } Else {
    $InitialSize = 1024+300
    $MaximumSize = 1024*3
    Set-PageFileSize -DL $DL -InitialSize $InitialSize -MaximumSize $MaximumSize
    }
    #>


    $InitialSize = (Get-CimInstance -ClassName Win32_PageFileUsage).AllocatedBaseSize
    $sum = $null
    (Get-Counter '\Process(*)\Page File Bytes Peak' -SampleInterval 15 -ErrorAction SilentlyContinue).CounterSamples.CookedValue | % {$sum += $_}
    $MaximumSize = ($sum*70/100)/1MB
    if ($Vol.FreeSpace -gt $MaximumSize) {
    Set-PageFileSize -DL $DL -InitialSize $InitialSize -MaximumSize $MaximumSize
    } else {
    Write-Warning -Message "Maximum size of page file being set exceeds the freespace available on the drive"
    }
    break

    }
    Default {
    if ($Vol.FreeSpace -gt $MaximumSize) {
    Set-PageFileSize -DL $DL -InitialSize $InitialSize -MaximumSize $MaximumSize
    } else {
    Write-Warning -Message "Maximum size of page file being set exceeds the freespace available on the drive"
    }
    }
    }
    }

    # Get current page file size information
    try {
    Get-CimInstance -ClassName Win32_PageFileSetting -ErrorAction Stop |Select-Object Name,
    @{Name="InitialSize(MB)";Expression={if($_.InitialSize -eq 0){"System Managed"}else{$_.InitialSize}}},
    @{Name="MaximumSize(MB)";Expression={if($_.MaximumSize -eq 0){"System Managed"}else{$_.MaximumSize}}}|
    Format-Table -AutoSize
    } catch {
    Write-Warning -Message "Failed to query Win32_PageFileSetting class because $($_.Exception.Message)"
    }
    If($Reboot) {
    Restart-Computer -ComputerName $Env:COMPUTERNAME -Force
    }
    }
    }
    End {}
    }

    Function Set-PageFileSize {
    [CmdletBinding()]
    Param(
    [Parameter(Mandatory)]
    [Alias('dl')]
    [ValidatePattern('^[A-Z]$')]
    [String]$DriveLetter,

    [Parameter(Mandatory)]
    [ValidateRange(0,[int32]::MaxValue)]
    [Int32]$InitialSize,

    [Parameter(Mandatory)]
    [ValidateRange(0,[int32]::MaxValue)]
    [Int32]$MaximumSize
    )
    Begin {}
    Process {
    #The AutomaticManagedPagefile property determines whether the system managed pagefile is enabled.
    #This capability is not available on windows server 2003,XP and lower versions.
    #Only if it is NOT managed by the system and will also allow you to change these.
    try {
    $Sys = Get-CimInstance -ClassName Win32_ComputerSystem -ErrorAction Stop
    } catch {

    }

    If($Sys.AutomaticManagedPagefile) {
    try {
    $Sys | Set-CimInstance -Property @{ AutomaticManagedPageFile = $false } -ErrorAction Stop
    Write-Verbose -Message "Set the AutomaticManagedPageFile to false"
    } catch {
    Write-Warning -Message "Failed to set the AutomaticManagedPageFile property to false in Win32_ComputerSystem class because $($_.Exception.Message)"
    }
    }

    # Configuring the page file size
    try {
    $PageFile = Get-CimInstance -ClassName Win32_PageFileSetting -Filter "SettingID='pagefile.sys @ $($DriveLetter):'" -ErrorAction Stop
    } catch {
    Write-Warning -Message "Failed to query Win32_PageFileSetting class because $($_.Exception.Message)"
    }

    If($PageFile){
    try {
    $PageFile | Remove-CimInstance -ErrorAction Stop
    } catch {
    Write-Warning -Message "Failed to delete pagefile the Win32_PageFileSetting class because $($_.Exception.Message)"
    }
    }
    try {
    New-CimInstance -ClassName Win32_PageFileSetting -Property @{Name= "$($DriveLetter):\pagefile.sys"} -ErrorAction Stop | Out-Null

    # http://msdn.microsoft.com/en-us/library/windows/desktop/aa394245%28v=vs.85%29.aspx
    Get-CimInstance -ClassName Win32_PageFileSetting -Filter "SettingID='pagefile.sys @ $($DriveLetter):'" -ErrorAction Stop | Set-CimInstance -Property @{
    InitialSize = $InitialSize ;
    MaximumSize = $MaximumSize ;
    } -ErrorAction Stop

    Write-Verbose -Message "Successfully configured the pagefile on drive letter $DriveLetter"

    } catch {
    Write-Warning "Pagefile configuration changed on computer '$Env:COMPUTERNAME'. The computer must be restarted for the changes to take effect."
    }
    }
    End {}
    }

    This is how the site said to execute:

    .\set-pagefile.ps1 -InitialSize 65536 -MaximumSize 65536 -DriveLetter "C:"

    Error I get from PDQ deploy:

    The term '.\set-pagefile.ps1 -InitialSize 65536 -MaximumSize 65536 -DriveLetter "C:"' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was
    included, verify that the path is correct and try again.

     

     

    0
    Comment actions Permalink
  • Stephen Valdinger

    I think I know what's wrong here. can you post a screenshot of your step settings?

    0
    Comment actions Permalink
  • Tim Moseley



    0
    Comment actions Permalink
  • Stephen Valdinger

    Yup, there's a a quotation issue. I can't quite see the full command but you are basically saying

    '.\Set-pagefile.ps1 -InitialSize 65536.......'

    You'll want to change it to:

    '.\Set-PageFile.ps1' -InitialSize 65536.........

    Them quotes can be pretty fickle at times!

    0
    Comment actions Permalink
  • Tim Moseley

    AWESOME!!  Thank you.  It worked.

    0
    Comment actions Permalink
  • Stephen Valdinger

    Glad to hear it! Happy Powershelling!

    0
    Comment actions Permalink
  • Tim Moseley

    Ok, I was wrong.   It doesn't error but it also doesn't change the settings.  It is still set to Automatic manage.  I need it to disable the automatic management and enable custom management with a min and max set to 65536 (3x physical memory). 

    I am doing this because it is suggested by Adobe as a possible solution to the computers slowing down or freezing when running Adobe Illustrator and Photoshop. I figured the best way would be a PS and push the settings to the labs.

    0
    Comment actions Permalink
  • Stephen Valdinger

    What OS(s) are you deploying this too?

     

    0
    Comment actions Permalink
  • Stephen Valdinger

    Just use this, it is way easier. Note, this is not my code entirely, I just found it online,dressed it up a little bit,  and tested it on my Windows 10 workstation. Does what it is supposed to as far as I can tell.

    Here's where I found it if you are interested: https://social.technet.microsoft.com/Forums/ie/en-US/557b0eea-c981-48fe-b830-8f9de0587c84/inconsistent-win32pagefilesetting?forum=winserverpowershell

    And some reading on the class itself so I could better understand what all it was doing: https://msdn.microsoft.com/en-us/library/aa394245(v=vs.85).aspx

    Function Set-PageFileSize {
    <#
    .SYNOPSIS
    Set the pagefile size using Powershell

    .EXAMPLE
    Set-PageFileSize -InitialSize 1234 -MaxSize 5678

    .EXAMPLE
    & './Set-PageFileSize.ps1' -InitialSize 1234 -MaxSize 5678

    #>

    Param($initialsize,$maxsize)

    # Set automanage pagefile to false; min/max pagefile size
    $computerSystem = Get-WmiObject -Class Win32_ComputerSystem -EnableAllPrivileges $computerSystem.AutomaticManagedPagefile = $false $computerSystem.Put() | Out-Null $pageFileSetting = Get-WmiObject -Class Win32_PageFileSetting $pageFileSetting.InitialSize = $initialsize $pageFileSetting.MaximumSize = $maxsize $pageFileSetting.Put() | Out-Null

    }

    0
    Comment actions Permalink
  • Tim Moseley

    This is deployed to Windows 10 - 1703.  Thank you for the new code.

    Tested - but when I go to the computer to check it is still managed by windows and the min and max aren't set.

    0
    Comment actions Permalink
  • Stephen Valdinger

    Well huh, that's rather interesting. I'll do some more testing on my end and see if I can replicate those results with a package. I'm wondering if the RunAs options are causing an issue. 

    I won't be able to circle back to this until tomorrow, but I'll see what I can come up with. 

    0
    Comment actions Permalink

Please sign in to leave a comment.