-
-
Notifications
You must be signed in to change notification settings - Fork 97
/
Enable_disk_write_caching.ps1
88 lines (75 loc) · 2.52 KB
/
Enable_disk_write_caching.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<#
.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