Powershell – set Pagefile
By: Date: August 6, 2021 Categories: Powershell Tags: , ,

You can use the below script to set the pagefile on a disk with Powershell. Just change the drive letter and the min and max size. Make sure you restart the machine after you ran the script.

# Specify the drive letter where the page file is located
$driveLetter = "C:"
# Specify the new minimum and maximum page file size in MB
$newMinSizeMB = 1024
$newMaxSizeMB = 1024 # Adjust this value as needed

# Delete the existing page file
Get-WmiObject -Query "SELECT * FROM Win32_PageFileSetting WHERE Name = '$driveLetter\\pagefile.sys'" | ForEach-Object {
    $_.Delete()
}

# Create a new page file
Set-WmiInstance -Class Win32_PageFileSetting -Arguments @{
    InitialSize = $newMinSizeMB
    MaximumSize = $newMaxSizeMB
    Name = "$driveLetter\\pagefile.sys"
}

Restart the machine with the following command.
-r = restart
-f = force
-t 0 = don’t wait.

shutdown -r -f -t 0

You can check the page files after the restarts with:

Get-WmiObject -Query "SELECT * FROM Win32_PageFileSetting"

Leave a Reply

Your email address will not be published. Required fields are marked *