Search code examples
powershelladb

PowerShell using variables to create adb push command


I'm trying to import file names and paths from a csv file to create individual adb push commands to push files to an Android device with the following script.

adb push "$($c.DirectoryName)\$($c.Name) $targetPath"

Write-Host adb push "$($c.DirectoryName)\$($c.Name) $targetPath"

The output of Write-Host gives me adb push C:\temp\Files\test.jpg /sdcard which works as a command if I copy and paste into PowerShell but my script does not work.

What am I doing wrong?

Thanks


Solution

  • It looks like you just got the quotation wrong. In general, only quote single argument string literals. If an argument is a single variable, then don't quote it because PowerShell already does the quoting, if necessary.

    Try this:

    adb push "$($c.DirectoryName)\$($c.Name)" $targetPath
    

    Actually you could just use the Fullname property so you don't need any quoting at all:

    adb push $c.Fullname $targetPath
    

    In your original code you passed "$($c.DirectoryName)\$($c.Name) $targetPath" as a single argument, due to the quoting, although the string actually contains multiple arguments.