Search code examples
powershellvariablesadb

Passing a powershell variable value to an adb shell command


Good day to all. After google-ing and trying various solutions, I'm a bit stuck with the following easy (at least it seemed so) task: I have a powershell variable, say:

$simpleString = "Hello World and stuff"

I'm testing an Android app via ADB, where I need to pass this variable's value as a string:

.\adb.exe shell input text "$simpleString"

I get error

.\adb.exe : Error: Invalid arguments for command: text

followed by reminder on how to use "input" command by adb.

Update: I've also tried the following workaround:

$myCmd = Write-output "adb.exe shell input text `"$simpleString`""

thus building a valid command for CMD and then run it via:

cmd /c $myCmd

but I still get same issue

Any help will be much appreciated, thank you.


Solution

  • OK, after some additional trial and error, I found a solution that worked for me (using back-ticks, single quotes and "wrapping" the command into another variable:

    $myCmd = Write-output "adb.exe shell input text `'$simpleString`'"
    & cmd /c $myCmd
    

    Spelling-out the first command for easier perception:

    $myCmd = Write-output {double quotes}adb.exe shell input text {back-tick}{single quote}$simpleString{back-tick}{single quote}{double quotes}