-
Notifications
You must be signed in to change notification settings - Fork 0
/
DisableHyper-V.bat
106 lines (86 loc) · 7.62 KB
/
DisableHyper-V.bat
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
@echo off
:: Set color scheme
color 0A
title Disable Hyper-V by @its-hritika
fltmc >nul 2>&1 || (
echo Administrator privileges are required.
PowerShell Start -Verb RunAs '%0' 2> nul || (
echo Right-click on the script and select "Run as administrator".
pause & exit 1
)
exit 0
)
:: Initialize environment
setlocal EnableExtensions DisableDelayedExpansion
:: ----------------------------------------------------------
:: ----------------Disable "Hypervisor" feature-----------------
:: ----------------------------------------------------------
echo --- Disable "Hypervisor" feature
bcdedit /set hypervisorlaunchtype off
echo.
Disable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V-Hypervisor
:: ----------------------------------------------------------
:: ----------------------------------------------------------
:: ----------------Disable "Hyper-V" feature-----------------
:: ----------------------------------------------------------
echo --- Disable "Hyper-V" feature
:: Disable the "Microsoft-Hyper-V-All" feature
PowerShell -ExecutionPolicy Unrestricted -Command "$featureName = 'Microsoft-Hyper-V-All'; $feature = Get-WindowsOptionalFeature -FeatureName "^""$featureName"^"" -Online -ErrorAction Stop; if (-Not $feature) {; Write-Output "^""Skipping: The feature `"^""$featureName`"^"" is not found. No action required."^""; Exit 0; }; if ($feature.State -eq [Microsoft.Dism.Commands.FeatureState]::Disabled) {; Write-Output "^""Skipping: The feature `"^""$featureName`"^"" is already disabled. No action required."^""; Exit 0; }; try {; Write-Host "^""Disabling feature: `"^""$featureName`"^""."^""; Disable-WindowsOptionalFeature -FeatureName "^""$featureName"^"" -Online -NoRestart -LogLevel ([Microsoft.Dism.Commands.LogLevel]::Errors) -WarningAction SilentlyContinue -ErrorAction Stop | Out-Null; } catch {; Write-Error "^""Failed to disable the feature `"^""$featureName`"^"": $($_.Exception.Message)"^""; Exit 1; }; Write-Output "^""Successfully disabled the feature `"^""$featureName`"^""."^""; Exit 0"
dism /online /disable-feature /featurename:microsoft-hyper-v-all
:: ----------------------------------------------------------
:: ----------------------------------------------------------
:: ------Disable "Hyper-V GUI Management Tools" feature------
:: ----------------------------------------------------------
echo --- Disable "Hyper-V GUI Management Tools" feature
:: Disable the "Microsoft-Hyper-V-Management-Clients" feature
PowerShell -ExecutionPolicy Unrestricted -Command "$featureName = 'Microsoft-Hyper-V-Management-Clients'; $feature = Get-WindowsOptionalFeature -FeatureName "^""$featureName"^"" -Online -ErrorAction Stop; if (-Not $feature) {; Write-Output "^""Skipping: The feature `"^""$featureName`"^"" is not found. No action required."^""; Exit 0; }; if ($feature.State -eq [Microsoft.Dism.Commands.FeatureState]::Disabled) {; Write-Output "^""Skipping: The feature `"^""$featureName`"^"" is already disabled. No action required."^""; Exit 0; }; try {; Write-Host "^""Disabling feature: `"^""$featureName`"^""."^""; Disable-WindowsOptionalFeature -FeatureName "^""$featureName"^"" -Online -NoRestart -LogLevel ([Microsoft.Dism.Commands.LogLevel]::Errors) -WarningAction SilentlyContinue -ErrorAction Stop | Out-Null; } catch {; Write-Error "^""Failed to disable the feature `"^""$featureName`"^"": $($_.Exception.Message)"^""; Exit 1; }; Write-Output "^""Successfully disabled the feature `"^""$featureName`"^""."^""; Exit 0"
:: ----------------------------------------------------------
:: ----------------------------------------------------------
:: --------Disable "Hyper-V Management Tools" feature--------
:: ----------------------------------------------------------
echo --- Disable "Hyper-V Management Tools" feature
:: Disable the "Microsoft-Hyper-V-Tools-All" feature
PowerShell -ExecutionPolicy Unrestricted -Command "$featureName = 'Microsoft-Hyper-V-Tools-All'; $feature = Get-WindowsOptionalFeature -FeatureName "^""$featureName"^"" -Online -ErrorAction Stop; if (-Not $feature) {; Write-Output "^""Skipping: The feature `"^""$featureName`"^"" is not found. No action required."^""; Exit 0; }; if ($feature.State -eq [Microsoft.Dism.Commands.FeatureState]::Disabled) {; Write-Output "^""Skipping: The feature `"^""$featureName`"^"" is already disabled. No action required."^""; Exit 0; }; try {; Write-Host "^""Disabling feature: `"^""$featureName`"^""."^""; Disable-WindowsOptionalFeature -FeatureName "^""$featureName"^"" -Online -NoRestart -LogLevel ([Microsoft.Dism.Commands.LogLevel]::Errors) -WarningAction SilentlyContinue -ErrorAction Stop | Out-Null; } catch {; Write-Error "^""Failed to disable the feature `"^""$featureName`"^"": $($_.Exception.Message)"^""; Exit 1; }; Write-Output "^""Successfully disabled the feature `"^""$featureName`"^""."^""; Exit 0"
:: ----------------------------------------------------------
:: ----------------------------------------------------------
:: -Disable "Hyper-V Module for Windows PowerShell" feature--
:: ----------------------------------------------------------
echo --- Disable "Hyper-V Module for Windows PowerShell" feature
:: Disable the "Microsoft-Hyper-V-Management-PowerShell" feature
PowerShell -ExecutionPolicy Unrestricted -Command "$featureName = 'Microsoft-Hyper-V-Management-PowerShell'; $feature = Get-WindowsOptionalFeature -FeatureName "^""$featureName"^"" -Online -ErrorAction Stop; if (-Not $feature) {; Write-Output "^""Skipping: The feature `"^""$featureName`"^"" is not found. No action required."^""; Exit 0; }; if ($feature.State -eq [Microsoft.Dism.Commands.FeatureState]::Disabled) {; Write-Output "^""Skipping: The feature `"^""$featureName`"^"" is already disabled. No action required."^""; Exit 0; }; try {; Write-Host "^""Disabling feature: `"^""$featureName`"^""."^""; Disable-WindowsOptionalFeature -FeatureName "^""$featureName"^"" -Online -NoRestart -LogLevel ([Microsoft.Dism.Commands.LogLevel]::Errors) -WarningAction SilentlyContinue -ErrorAction Stop | Out-Null; } catch {; Write-Error "^""Failed to disable the feature `"^""$featureName`"^"": $($_.Exception.Message)"^""; Exit 1; }; Write-Output "^""Successfully disabled the feature `"^""$featureName`"^""."^""; Exit 0"
:: ----------------------------------------------------------
:: ----------------------------------------------------------
:: -Disable "Hyper-V net Adapters" feature--
:: ----------------------------------------------------------
echo --- Disable "Hyper-V net adapters" feature
get-netadapter|where-object {$_.interfacedescription -like "*hyper-v*"}|Disable-NetAdapter
REM Define the registry key paths
set "key1=HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\DeviceGuard"
set "key2=HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa"
set "key3=HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\DeviceGuard\Scenarios\HypervisorEnforcedCodeIntegrity"
REM Define the value names
set "value1=EnableVirtualizationBasedSecurity"
set "value2=LsaCfgFlags"
set "value3=Enabled"
REM Set the value data
set "data1=0"
set "data2=0"
set "data3=0"
REM Add the first registry entry
reg add "%key1%" /v "%value1%" /t REG_DWORD /d %data1% /f >nul 2>&1
REM Add the second registry entry
reg add "%key2%" /v "%value2%" /t REG_DWORD /d %data2% /f >nul 2>&1
REM Modify the value data of the third registry entry
reg add "%key3%" /v "%value3%" /t REG_DWORD /d %data3% /f >nul 2>&1
REM Check if the registry entries were added/modified successfully
if %errorlevel% equ 0 (
echo Registry entries added/modified successfully.
) else (
echo Failed to add/modify registry entries.
)
:: Pause the script to view the final state
pause
:: Restore previous environment settings
endlocal
:: Exit the script successfully
exit /b 0