Using Powershell to Run Diskpart Commands

Using Powershell to Run Diskpart Commands

Although there is a Get-Disk cmdlet in PowerShell, it is not recognized as a command in PowerShell 2.0 on Windows 7. So, I use the diskpart command with the following as an example:

DiskPart
List disk
Select disk 1
Clean
Create partition primary
Select partition 1
Active
Format FS=NTFS quick
Assign

I want to know if I can write those lines to a text file and then use PowerShell to execute them. I couldn't find a handy guide for that. How can I use PowerShell to run Win32 commands?

1

1 Answer

Here's a relevant document from Microsoft: diskpart scripts and examples.

Use diskpart /s to run scripts that automate disk-related tasks, such as creating volumes or converting disks to dynamic disks. [...]

To create a diskpart script, create a text file that contains the Diskpart commands that you want to run, with one command per line, and no empty lines. You can start a line with rem to make the line a comment. For example, here's a script that wipes a disk and then creates a 300 MB partition for the Windows Recovery Environment:

select disk 0
clean
convert gpt
create partition primary size=300
format quick fs=ntfs label=Windows RE tools
assign letter=T

To run a diskpart script, at the command prompt, type the following command, where scriptname is the name of the text file that contains your script:

diskpart /s scriptname.txt
5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Alexander Ross
Author

Alexander Ross

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.