Sometimes, you need to call commands that require interactive input to work. For example, to find out existing drives with DiskPart, you would have to start DiskPart and then interactively type the command:
A little known feature of PowerShells pipeline is the ability to feed required input into commands that would otherwise expect input to be entered interactively. So to get a list of drives without interaction, use this line:
You can then process the results with Where-Object and filter out any unwanted information. For example:
Be aware that DiskPart requires Admin privileges. On Vista and above with UAC, make sure you launch PowerShell with elevated rights in the first place or else DiskPart will elevate itself and break the pipeline.
This feature works for all commands requiring interactive input, and you can even submit more than one command:
select disk 0
'@ | DiskPart