Skip to content

Instantly share code, notes, and snippets.

@trevor229
Last active July 26, 2024 23:52
Show Gist options
  • Save trevor229/9a685fa4929357b3993d071fe38bee43 to your computer and use it in GitHub Desktop.
Save trevor229/9a685fa4929357b3993d071fe38bee43 to your computer and use it in GitHub Desktop.
Diskpart Automation Script
:: Windows diskpart batchfile for easily formatting, creating a primary partition with a label, and attaching the disk to user defined values
:: https://github.com/trevor229
@echo off
net session >nul 2>&1
if %errorLevel% == 0 (
goto :drive_handler
) else (
@echo Failure: Script must be run as Administrator.
pause
exit
)
:drive_handler
@echo list disk > listdisk.txt
diskpart /S listdisk.txt > listoutput.txt
del listdisk.txt
@echo Diskpart Output
@echo.
type listoutput.txt | findstr /v Microsoft | findstr /v computer | findstr /v "^$"
@echo.
set /P diskNum="What disk do you want to format? "
set /P confirmFlag="Are you sure you want to format disk %diskNum%? "
if %confirmFlag% == y (
goto :diskpart_cmds
) else (
cls
goto :drive_handler
)
:diskpart_cmds
set /P diskLabel="What should the disk be called? (No spaces) "
set /P diskLetter="What letter should the disk be attached to? "
@echo select disk %diskNum% > diskpartcmd.txt
@echo clean >> diskpartcmd.txt
@echo create partition primary >> diskpartcmd.txt
@echo select partition 1 >> diskpartcmd.txt
@echo active >> diskpartcmd.txt
@echo format FS=NTFS label=%diskLabel% quick >> diskpartcmd.txt
@echo assign letter=%diskLetter% >> diskpartcmd.txt
diskpart /S diskpartcmd.txt
del diskpartcmd.txt
del listoutput.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment