使用注册键/命令行在 Windows 10/8/7 中打开或关闭磁盘写入缓存

使用注册键/命令行在 Windows 10/8/7 中打开或关闭磁盘写入缓存

据微软称,在 Windows 10/8/7 中,打开或关闭磁盘写入缓存

  1. 右键单击“我的电脑”,然后单击“属性”。
  2. 单击“硬件”选项卡,然后单击“设备管理器”。
  3. 展开磁盘驱动器。
  4. 右键单击要打开或关闭磁盘写入缓存的驱动器,然后单击“属性”。
  5. 单击“策略”选项卡。
  6. 根据需要单击以选中或清除“在磁盘上启用写入缓存”复选框。
  7. 单击“确定”。

在此处输入图片描述

如何使用注册表项 (regedit)/命令行打开或关闭磁盘写入缓存?(在多台电脑上自动执行该过程)

PD:有一个C/C++ 中的选项,但已过时或不适用。同样适用于dskcache 使用的方法

答案1

微软发布的旧版 diskcache.exe 似乎已丢失。请尝试使用我很久以前制作的兼容工具。

https://www.vector.co.jp/soft/winnt/hardware/se487753.html

要在磁盘 0 上启用写缓存,

.\diskcach 0 -w 1

禁用

.\diskcach 0 -w 0

它可能需要 MSVC++ 2008 SP1 可再发行包。

答案2

我创建了一个功能,仅用于启用/禁用系统驱动器的写入磁盘缓存。需要重新启动。

永久链接:https://github.com/farag2/Utilities/blob/master/Enable_disk_write_caching.ps1

<#
    .SYNOPSIS
    Configure the disk write caching

    .PARAMETER Disable
    Disable the disk write caching

    .PARAMETER Enable
    Enable the disk write caching

    .EXAMPLE
    DiskWriteCaching -Disable

    .EXAMPLE
    DiskWriteCaching -Enable

    .NOTES
    Current user
#>
function DiskWriteCaching
{
    param
    (
        [Parameter(
            Mandatory = $true,
            ParameterSetName = "Disable"
        )]
        [switch]
        $Disable,

        [Parameter(
            Mandatory = $true,
            ParameterSetName = "Enable"
        )]
        [switch]
        $Enable
    )

    # Get system drive ID regardless of the port number
    $Index = (Get-Partition | Where-Object -FilterScript {$_.DriveLetter -eq $env:SystemDrive[0]}).DiskNumber
    $SystemDriveID = (Get-CimInstance -ClassName CIM_DiskDrive | Where-Object -FilterScript {$_.Index -eq $Index}).PNPDeviceID
    # Get system drive instance
    $PSPath = (Get-ChildItem -Path HKLM:\SYSTEM\CurrentControlSet\Enum\SCSI | Where-Object -FilterScript {$SystemDriveID -match $_.PSChildName}).PSPath
    # We need to go deeper... LeonardoDiCaprio.jpg
    $PSPath = (Get-ChildItem -Path $PSPath | Where-Object -FilterScript {$SystemDriveID -match $_.PSChildName}).PSPath

    # Check whether disk write caching is enabled
    $IsDeviceCacheEnabled = (Get-StorageAdvancedProperty -PhysicalDisk (Get-PhysicalDisk | Where-Object -FilterScript {$_.DeviceID -eq $Index})).IsDeviceCacheEnabled

    switch ($PSCmdlet.ParameterSetName)
    {
        "Disable"
        {
            if ($IsDeviceCacheEnabled)
            {
                if (-not (Test-Path -Path "$PSPath\Device Parameters\Disk"))
                {
                    # Create "Disk" folder
                    New-Item -Path "$PSPath\Device Parameters\Disk" -Force
                }

                # Disable disk write caching
                New-ItemProperty -Path "$PSPath\Device Parameters\Disk" -Name UserWriteCacheSetting -PropertyType DWord -Value 0 -Force
                New-ItemProperty -Path "$PSPath\Device Parameters\Disk" -Name CacheIsPowerProtected -PropertyType DWord -Value 0 -Force
            }
        }
        "Enable"
        {
            if (-not $IsDeviceCacheEnabled)
            {
                if (-not (Test-Path -Path "$PSPath\Device Parameters\Disk"))
                {
                    # Create "Disk" folder
                    New-Item -Path "$PSPath\Device Parameters\Disk" -Force
                }

                # Enable disk write caching
                New-ItemProperty -Path "$PSPath\Device Parameters\Disk" -Name UserWriteCacheSetting -PropertyType DWord -Value 1 -Force
                New-ItemProperty -Path "$PSPath\Device Parameters\Disk" -Name CacheIsPowerProtected -PropertyType DWord -Value 0 -Force
            }
        }
    }

    Write-Warning "Make sure to restart your PC!"
}

# DiskWriteCaching -Disable
# DiskWriteCaching -Enable

相关内容