Windows Server 2012 2016 2019 2022 RAM Disk Setup Guide 🌱

NOTE: RAM disks by their very nature are cleared on reboot. Do not use them to store any data that cannot be lost. A suitable use case would be temporary session files for a web server

Installing iSCSI Target Server

  1. Launch Server Manager if it does not automatically load
    1. Click the Start button > Server Manager
  2. Click Manage > Add roles and features
  3. Click Next on the Before you begin screen
  4. Select Role-based or feature-based installation > Next
  5. Leave Select a server from the server pool selected and select the current server > Next
  6. Expand File and Storage Services > Expand File and iSCSI Services > Select iSCSI Target Server
  7. A popup will appear with additional required roles and features, click the Add Features button
  8. Click Next
  9. Click Next on the Select features screen
  10. Click Install on the confirmation screen
  11. Leave the installation progress screen open until the install completes

Creating the RAM Disk

  1. Launch Powershell as administrator
  2. Enter the following commands to create the RAM disk, replacing the IP address with the current server's IP address
    # allow iSCSI loopback registry setting
    Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\iSCSI Target' -Name AllowLoopBack -Value 1
    # start the iscsi initiator service
    Start-Service -Name MSiSCSI
    # set the iscsi initiator service to start automatically
    Set-Service -Name MSiSCSI -StartupType Automatic
    # create the ram disk, changing size as needed
    New-IscsiVirtualDisk -Path "ramdisk:RAMDisk.vhdx" -Size 256MB
    # initialize iSCSI target
    New-IscsiServerTarget -TargetName RAMDisk -InitiatorIds @("IPAddress:192.168.0.65")
    # connect to the ram disk
    Add-IscsiVirtualDiskTargetMapping -TargetName RAMDisk -DevicePath "ramdisk:RAMDisk.vhdx"
  3. Back in Server Manager, select Tools > iSCSI Initiator
  4. Enter the IP address in the Target field > Click Quick Connect...
  5. Click Done
  6. Back in Powershell, run the following command
    # initialize the disk as NTFS
    Get-Disk | Where partitionstyle -eq 'raw' | Initialize-Disk -PartitionStyle MBR -PassThru | New-Partition -DriveLetter R -UseMaximumSize | Format-Volume -FileSystem NTFS -NewFileSystemLabel "RAMDisk" -Confirm:$false
  7. Open File Explorer and the new RAM disk should be listed as a usable NTFS drive

Re-creating the RAM Disk on Boot

After the RAM disk has been setup initially, follow the steps below to have it recreated on system boot.

  1. Open a text editor
  2. Paste the following Powershell commands, changing the size as needed

    New-IscsiVirtualDisk -Path "ramdisk:RAMDisk.vhdx" -Size 256MB
    Add-IscsiVirtualDiskTargetMapping -TargetName RAMDisk -DevicePath "ramdisk:RAMDisk.vhdx"
    Get-Disk | Where partitionstyle -eq 'raw' | Initialize-Disk -PartitionStyle MBR -PassThru | New-Partition -DriveLetter R -UseMaximumSize | Format-Volume -FileSystem NTFS -NewFileSystemLabel "RAMDisk" -Confirm:$false

  3. Save the file as ramdisk.ps1 to a safe location to run from, c:\scripts for example
  4. Click on the Start Button > Type task > Launch Task Scheduler
  5. Right click the Task Scheduler Library folder in the left pane > Create Basic Task...
  6. Set the name to RAM Disk and optionally set a Description > Click Next
  7. For the Trigger, select When the computer starts > Click Next
  8. For the Action, select Start a program > Click Next
  9. In the Program/script field, paste the following:

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

  10. In the Add arguments field, paste the following, editing the path to the .ps1 file if necessary:

    "C:\Scripts\ramdisk.ps1"

  11. Click Next
  12. Check the Open the Properties dialog for this task when I click Finish box
  13. Click Finish
  14. In the Properties dialog, click the Change User or Group... button
  15. Type system > Press Enter
  16. Check the Run with highest privileges box
  17. Click OK to create the scheduled task
  18. Reboot the server to verify the RAM disk is re-created automatically