2

I want to use PowerShell to automate diskpart.exe, but the script arguments that PowerShell builds are seen as invalid. For example, the trivial diskpart script below works as expected:

$diskDetailsDPCom=@(
    "select disk 0",
    "detail disk",
    "exit"
)
$diskDetailsDPCom | diskpart

But when I try to build the “select disk” command by concatenating a variable or literal “0”, then diskpart fails with The arguments specified for this command are not valid.:

$diskDetailsDPCom=@(
    "select disk " + "0",
    "detail disk",
    "exit"
)
$diskDetailsDPCom | diskpart

What is going on here, and how might I proceed?

4
  • 2
    "select disk " + “0” -> ("select disk " + “0”) Commented Oct 12, 2016 at 16:59
  • Some of those quotes are MS-Word's "smart quotes". Is that intentional? Commented Oct 12, 2016 at 17:01
  • Smart quotes were due to a new Word install, and lazyness. Thanks again! Commented Oct 12, 2016 at 17:34
  • Not to worry. PowerShell understands smart quotes just fine, but they do break a lot of other command-line programs. Commented Oct 12, 2016 at 17:57

2 Answers 2

5

In PowerShell the comma (,) operator has higher precedence than the concatenation (+) operator. Your two blocks of code are evaluating differently.

@("select disk 0", “detail disk”, “exit”)

evaluates to an array with three strings as you expect. However, your second example actually parses as

@("select disk " + (“0”, “detail disk”, “exit”))

To do the concatenation, PowerShell converts the array to a string with the -join operator, and your result is a string "select disk 0 detail disk exit", wrapped in an array.

To get the array you want, put parentheses around the concatenation.

@(
    ("select disk " + “0”),
    “detail disk”,
    “exit”
)
Sign up to request clarification or add additional context in comments.

1 Comment

That was my problem.Thanks!
-1

Amended script. The splatting won't work but the array part should still be correct, as per @ryan bemrose's answer

$diskDetailsDPCom=@(
    "select disk", "0"
    , "detail disk"
   , "exit"
)
@diskDetailsDPCom | diskpart

Non-working answer below.

Instead of piping the args into diskpart try this...

$diskDetailsDPCom=@(
    "select disk", "0"
    , "detail disk"
   , "exit"
)
diskpart @diskDetailsDPCom

...where PowerShell will splat the array of args against diskpart.

Note that any args that need spaces between them are separate array elements. If using a switch such as assign letter=R, letter=R would be one array element.

This is untested against diskpart, but I have successfully used this method of splatting an array of args on the left hand side of an external command, rather than piping in and it works.

1 Comment

Sorry, diskpart is old and weird, and it does not take command-line arguments, it runs "scripts". So commands must be piped in. I tried your suggestion, and diskpart "was unable to process the parameters"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.