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
AssignI 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?
11 Answer
Here's a relevant document from Microsoft: diskpart scripts and examples.
5Use
diskpart /sto 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
remto 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=TTo 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