Take the script code below. It is recommended to use the third or fifth version of the code. Copy it into Notepad. Change the suffix of Notepad to bat file. Put this file in the same directory as the vhdx file and double-click to run it.
Why should I use a virtual disk:
1. Used to shit some software. When you use some domestic software, have you ever encountered the phenomenon of them pooping (creating folders randomly) in the root directory of your disk? These softwares only select the disk with the largest capacity to poop. It happens that the virtual disk of Windows is the largest. Can be set to 64TB
2. Unrivaled security. Some important learning videos and personal information are placed in a virtual disk encrypted with bitlocker. Normally the disk is not mounted to the system, and the disk cannot be seen in the resource manager at all, even if the virtual disk file is discovered and mounted. Once it is installed, with bitlocker present, it is impossible to access the disk and view the files inside without knowing the password. Since discovering this method, it seems that I have created a warm and safe villa for young ladies? No, it’s a private document warehouse [serious]. . .
3. Portability. You only need to double-click to mount it and it can be used as a normal disk. There is no difference. With the automatic mounting script at startup, you don’t have to worry about it at all. Before it is mounted, it is just like an ordinary file. You can copy and move it as a whole at any time, and it is also convenient for backup everywhere.
================================================== =====
I digress. .
Automatically mount virtual disk script code:
How to automatically mount a virtual disk? Use diskpart tool. First use the command to create the mount disk instruction file used by diskpart, and use diskpart to execute the instructions in this file.
First version: Simple version
echo select vdisk "C:\File path\File name.vhdx" > temp_diskpart_script.txt echo attach vdisk >> temp_diskpart_script.txt :: Run diskpart to execute temporary scripts diskpart /s temp_diskpart_script.txt :: Delete temporary script files del temp_diskpart_script.txt
This is the first version. Mail needs to be run with administrator rights (troublesome). In addition, the measured script file is placed in the same directory as the virtual disk so that there will be no mounting unresponsive bug, so there is a second version.
Second Edition: Automatically find disk mounts in the current directory
setlocal enabledelayeexpansion :: Find files with the suffix .vhdx in the current directory set "search_dir=%~dp0" set "extension=.vhdx" set "vhdx_file=" for %%f in ("%search_dir%*%extension%") do ( set "vhdx_file=%%~nxf" goto:MountDisk ) :MountDisk if not defined vhdx_file ( echo No file with suffix .vhdx found goto :End ) set DiskFile=%vhdx_file% set DiskLabel=MyVirtualDisk :: Create a temporary script file containing the diskpart command echo select vdisk file="%~dp0%DiskFile%" > temp_diskpart_script.txt echo attach vdisk >> temp_diskpart_script.txt :: Run diskpart to execute temporary scripts diskpart /s temp_diskpart_script.txt :: Delete temporary script files del temp_diskpart_script.txt echo The disk is mounted to %DiskLabel% :End endlocal
The bat file is placed in the same directory as the virtual disk. The script detects the virtual disk file in the current directory. If detected, it directly jumps to the mount command. A for loop is set up here. All virtual disk files in this directory will be mounted.
Then there is the trouble of needing to right-click and run with administrator privileges. Just add the cmd privilege escalation code I posted in my previous article, and the complete and usable version of the bat script will come out:
Version 3: No need to run as administrator, just double-click to use
@echo off ::BatchGotAdmin ::---------------------------------------- REM --> Check for permissions >nul 2> & amp;1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system" REM --> If error flag set, we do not have admin. if '%errorlevel%' NEQ '0' ( echo Requesting administrative privileges... gotoUACPrompt ) else ( goto gotAdmin ) :UACPrompt echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs" echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs" "%temp%\getadmin.vbs" exit/B :gotAdmin if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" ) pushd "?%" CD /D "%~dp0" setlocal enabledelayeexpansion :: Find files with the suffix .vhdx in the current directory set "search_dir=%~dp0" set "extension=.vhdx" set "vhdx_file=" for %%f in ("%search_dir%*%extension%") do ( set "vhdx_file=%%~nxf" goto:MountDisk ) :MountDisk if not defined vhdx_file ( echo No file with suffix .vhdx found goto :End ) set DiskFile=%vhdx_file% set DiskLabel=MyVirtualDisk :: Create a temporary script file containing the diskpart command echo select vdisk file="%~dp0%DiskFile%" > temp_diskpart_script.txt echo attach vdisk >> temp_diskpart_script.txt :: Run diskpart to execute temporary scripts diskpart /s temp_diskpart_script.txt :: Delete temporary script files del temp_diskpart_script.txt echo The disk is mounted to %DiskLabel% :End endlocal
↑↑↑↑↑↑↑↑↑This version is suitable for placing it directly in the startup folder or in the task scheduler to allow the system to automatically mount the disk at startup
The code is written here. For the sake of security, the command line will not disappear after mounting, and the virtual disk will automatically pop up after I press any key on the command line.
Version 4: Add automatic disk ejection code
@echo off ::BatchGotAdmin ::---------------------------------------- REM --> Check for permissions >nul 2> & amp;1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system" REM --> If error flag set, we do not have admin. if '%errorlevel%' NEQ '0' ( echo Requesting administrative privileges... gotoUACPrompt ) else ( goto gotAdmin ) :UACPrompt echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs" echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs" "%temp%\getadmin.vbs" exit/B :gotAdmin if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" ) pushd "?%" CD /D "%~dp0" setlocal enabledelayeexpansion :: Find files with the suffix .vhdx in the current directory set "search_dir=%~dp0" set "extension=.vhdx" set "vhdx_file=" for %%f in ("%search_dir%*%extension%") do ( set "vhdx_file=%%~nxf" goto:MountDisk ) :MountDisk if not defined vhdx_file ( echo No file with suffix .vhdx found goto :End ) set DiskFile=%vhdx_file% set DiskLabel=MyVirtualDisk :: Create a temporary script file containing the diskpart command echo select vdisk file="%~dp0%DiskFile%" > temp_diskpart_script.txt echo attach vdisk >> temp_diskpart_script.txt :: Run diskpart to execute temporary scripts diskpart /s temp_diskpart_script.txt :: Delete temporary script files del temp_diskpart_script.txt echo The disk is mounted to %DiskLabel% :: Wait for user input pause :: Unmount (eject) a virtual disk echo Unmount virtual disk %DiskLabel% echo select vdisk file="%~dp0%DiskFile%" > temp_diskpart_script.txt echo detach vdisk >> temp_diskpart_script.txt diskpart /s temp_diskpart_script.txt del temp_diskpart_script.txt echo The virtual disk has been unmounted from %DiskLabel% :End endlocal
If the virtual disk has BitLocker, you can enter the password in the terminal where the script is executed and then unlock BitLocker and then automatically open the virtual disk. The code is as follows
Fifth Edition Full Version: One-click automatic mounting, enter password to unlock BitLocker, automatic pop-up
@echo off ::BatchGotAdmin ::--------------------------------------------- REM --> Check for permissions >nul 2> & amp;1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system" REM --> If error flag set, we do not have admin. if '%errorlevel%' NEQ '0' ( echo Requesting administrative privileges... gotoUACPrompt ) else ( goto gotAdmin ) :UACPrompt echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs" echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs" "%temp%\getadmin.vbs" exit/B :gotAdmin if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" ) pushd "?%" CD /D "%~dp0" setlocal enabledelayeexpansion :: Find files with the suffix .vhdx in the current directory set "search_dir=%~dp0" set "extension=.vhdx" set "vhdx_file=" for %%f in ("%search_dir%*%extension%") do ( set "vhdx_file=%%~nxf" goto:MountDisk ) :MountDisk if not defined vhdx_file ( echo No file with suffix .vhdx found goto :End ) set DiskFile=%vhdx_file% set DiskLabel=MyVirtualDisk :: Create a temporary script file containing the diskpart command echo select vdisk file="%~dp0%DiskFile%" > temp_diskpart_script.txt echo attach vdisk >> temp_diskpart_script.txt :: Run diskpart to execute temporary scripts diskpart /s temp_diskpart_script.txt :: Delete temporary script files del temp_diskpart_script.txt echo The disk is mounted to %DiskLabel% ::Enter BitLocker password Enter echo to unlock this secret disk hidden in the computer manage-bde -unlock X: -password ::Open disk start X:\ :: Wait for user input echo Press any key to unplug this very, very, very awesome disk pause :: Unmount (eject) a virtual disk echo Unmount virtual disk %DiskLabel% echo select vdisk file="%~dp0%DiskFile%" > temp_diskpart_script.txt echo detach vdisk >> temp_diskpart_script.txt diskpart /s temp_diskpart_script.txt del temp_diskpart_script.txt echo The virtual disk has been unmounted from %DiskLabel% :End endlocal
over.
================================================== ==============
Add a little something else:
After files are deleted from the virtual disk (the kind that is also cleared from the recycle bin), the vhdx virtual disk file will not become smaller, so you need to use the function of cleaning up the virtual disk in hyper-v. The code with the privilege escalation code is as follows: The bat script can be run directly by placing it in the virtual disk file directory. You can add this code in front of the bat that automatically mounts the virtual disk at boot. ?
Clean compressed virtual disk
echo privilege escalation ::BatchGotAdmin ::------------------------------------------ REM --> Check for permissions >nul 2> & amp;1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system" REM --> If error flag set, we do not have admin. if '%errorlevel%' NEQ '0' ( echo Requesting administrative privileges... gotoUACPrompt ) else ( goto gotAdmin ) :UACPrompt echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs" echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs" "%temp%\getadmin.vbs" exit/B :gotAdmin if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" ) pushd "?%" CD /D "%~dp0" echo Privilege escalation completed emmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm ::↑Elevation of rights setlocal enabledelayeexpansion :: Find files with the suffix .vhdx in the current directory set "search_dir=%~dp0" set "extension=.vhdx" set "vhdx_file=" for %%f in ("%search_dir%*%extension%") do ( set "vhdx_file=%%~nxf" goto:MountDisk ) :MountDisk if not defined vhdx_file ( echo No file with suffix .vhdx found goto :End ) set DiskFile=%vhdx_file% set DiskLabel=MyVirtualDisk :: Create a temporary script file containing the diskpart command echo select vdisk file="%~dp0%DiskFile%" > temp_diskpart_script.txt echo attach vdisk readonly >> temp_diskpart_script.txt :: Run diskpart to execute temporary scripts diskpart /s temp_diskpart_script.txt :: Delete temporary script files del temp_diskpart_script.txt echo The disk is mounted (read-only) to %DiskLabel% :: Create a PowerShell script file echo Optimize-VHD -Path "%~dp0%DiskFile%" -Mode Full > OptimizeVHD.ps1 ::Run a PowerShell script powershell -ExecutionPolicy Bypass -File OptimizeVHD.ps1 :: Delete PowerShell script files del OptimizeVHD.ps1 :: Unmount (eject) a virtual disk echo Unmount virtual disk %DiskLabel% echo select vdisk file="%~dp0%DiskFile%" > temp_diskpart_script.txt echo detach vdisk >> temp_diskpart_script.txt diskpart /s temp_diskpart_script.txt del temp_diskpart_script.txt echo virtual disk %DiskLabel% has been successfully compressed to reclaim space and unmounted :End endlocal
No more
The purpose of writing this article is to record the tips I have researched in my daily use of computers in order to use the Windows system more conveniently. Many times I want to implement some automated functions in the system and it is really convenient to implement them through bat files. , when the bat script is running, the code scrolling in the black frame is really full. Of course, if you don’t like the black frame, you can also make it not display the black frame.