3 Autostart scrcpy on USB connection with console output with pwsh7 script
Helge Kosuch edited this page 2026-10-01 12:12:36 +02:00

Prerequisites:

  • autoadb
  • powershell7, which is multi-platform

I tested this on Win10.

autoadb-with-output.ps1:

<#
.SYNOPSIS
    Starts AutoADB with custom scrcpy parameters.

.DESCRIPTION
    Captures all passed arguments and forwards them to scrcpy.
    The '-s {}' parameter is automatically added by AutoADB and must not be passed manually.

.EXAMPLE
    .\autoadb-with-output.ps1 --no-audio --stay-awake --window-x=700 --window-y=1 --max-size=1168 --window-height=1050
#>

[CmdletBinding()]
param(
    [Parameter(ValueFromRemainingArguments = $true)]
    [string[]]$ScrcpyArgs
)

# 1. Display help if no arguments were provided
if ($null -eq $ScrcpyArgs -or $ScrcpyArgs.Count -eq 0) {
    Write-Host "Usage:" -ForegroundColor Yellow
    Write-Host "  .\autoadb-with-output.ps1 <scrcpy-parameters>"
    Write-Host ""
    Write-Host "Example:" -ForegroundColor Yellow
    Write-Host "  .\autoadb-with-output.ps1 --no-audio --stay-awake --window-x=700 --window-y=1 --max-size=1168 --window-height=1050"
    exit
}

# 2. Terminate previous instances
Stop-Process -Name autoadb, scrcpy -Force -ErrorAction SilentlyContinue

# 3. Define paths
$autoAdbPath = 'L:\Dropbox\progs\autoadb.exe'
$scrcpyPath  = 'C:\progs\scrcpy\scrcpy.exe'

# 4. Combine passed arguments with the required '-s {}' for AutoADB
$fullArgs = @($scrcpyPath) + $ScrcpyArgs + @('-s', '{}')

# 5. Start AutoADB in the background
$job = & $autoAdbPath @fullArgs &

# 6. Capture live output in the terminal
$action = {
    $line = $Sender[$EventArgs.Index]
    [Console]::WriteLine("[scrcpy] $line")
}

$sub = Register-ObjectEvent -InputObject $job.ChildJobs[0].Output -EventName DataAdded -Action $action

Write-Host "AutoADB has been started (Job ID: $($job.Id))." -ForegroundColor Green