🧠 Mirroring Android – PowerShell

New to PowerShell? Jump to: PowerShell in a Nutshell (below).


πŸ“„ Mirroring Android Phone on Windows

βœ… Prerequisite

  • adb and scrcpy must be installed and in your PATH (or specify their full paths).

πŸ”§ 1. Install adb (Android Debug Bridge)

Option A: Use Google’s Platform Tools (Recommended)

  1. Go to the official page:
    https://developer.android.com/tools/releases/platform-tools
  2. Download the ZIP for Windows:
    • File: platform-tools-latest-windows.zip
  3. Extract the ZIP to a known location, e.g.: makefileCopyEditC:\Android\platform-tools
  4. Add it to your system PATH:
    • Press Win + S, search for Environment Variables.
    • Click “Edit the system environment variables”.
    • Click “Environment Variables…”.
    • Under System variables, select Path, then Edit…
    • Click New, add: makefileCopyEditC:\Android\platform-tools
    • Click OK on all dialogs.
  5. Open a new PowerShell window and test: adb version

πŸ“Ί 2. Install scrcpy (Android Screen Mirroring)

Option A: Use Prebuilt Windows Release (Recommended)

  1. Visit the official scrcpy GitHub page:
    https://github.com/Genymobile/scrcpy/releases
  2. Download the latest .zip file, e.g.:
    scrcpy-win64-v2.3.1.zip (or whatever is current)
  3. Extract it to a known location, e.g.: C:\Android\scrcpy
  4. Add to PATH (same as above), e.g.: C:\Android\scrcpy
  5. Test it in PowerShell: scrcpy --version

🧹 Optional Cleanup

You can put both platform-tools and scrcpy in the same C:\Android folder for simplicity:

C:\Android\platform-tools
C:\Android\scrcpy

And make sure both are added to your PATH.


βœ… You’re Done!

Now you can run:

adb devices
scrcpy
scrcpy --tcpip

PowerShell Script

Below is a PowerShell script that guides the user step-by-step and uses adb and scrcpy to mirror an Android phone’s screen. It includes prompts, checks, and optional pairing memory if feasible.


βœ… Requirements

  • adb and scrcpy must be in your PATH (or specify their full paths).
  • Android Developer Options are already enabled on the phone.
  • USB Debugging must be enabled manually on the phone before running this.
  • Windows will remember USB pairing after you accept it once on the phone, once authorized, it stays.

πŸ“œ PowerShell Script: mirror_android.ps1

# mirror_android.ps1

function Prompt-Wait($message) {
    Write-Host "`n$message" -ForegroundColor Cyan
    Read-Host "Press Enter to continue"
}

function Check-Command($cmd) {
    if (-not (Get-Command $cmd -ErrorAction SilentlyContinue)) {
        Write-Error "Required command '$cmd' not found in PATH."
        exit 1
    }
}

# Check dependencies
Check-Command adb
Check-Command scrcpy

# Step 1: Prompt for USB Debug mode
Prompt-Wait "Enable USB Debugging on your Android phone (Settings β†’ Developer Options β†’ USB Debugging)"

# Step 2: Prompt to connect USB
Prompt-Wait "Connect your Android phone to the PC via USB"

# Step 3: Start adb server
Write-Host "`nStarting adb..." -ForegroundColor Yellow
adb start-server | Out-Null

# Step 4: Wait for device
Write-Host "Waiting for device authorization..."
$null = adb wait-for-device

# Optional: Show connected devices
$devices = adb devices | Select-String -Pattern "device$"
if ($devices.Count -eq 0) {
    Write-Error "No authorized device found. Check your phone and accept the USB Debugging prompt."
    exit 1
}
Write-Host "Device connected and authorized." -ForegroundColor Green

# Step 5: Start scrcpy
Write-Host "`nLaunching screen mirror using scrcpy..." -ForegroundColor Yellow
Start-Process -NoNewWindow scrcpy

# Step 6: Inform about USB disconnection
Prompt-Wait "You may now interact with your phone on screen. When finished, you can close the window and disconnect the USB."

# Optional: Clean exit
adb kill-server
Write-Host "adb server stopped."

πŸ“ Notes

  • PowerShell can’t “automatically pair” via USB debugging β€” you must accept the RSA prompt on the phone screen once. After that, the PC is remembered until USB debugging is revoked/reset.
  • Windows stores authorized adb keys under:
    • C:\Users\<username>\.android\adbkey

πŸš€ How to Use

  1. Save as mirror_android.ps1
  2. Open PowerShell as administrator
  3. Run: .\mirror_android.ps1

PowerShell in a Nutshell

PowerShell is:

  • A shell and scripting language developed by Microsoft.
  • Built on .NET, so it can use .NET classes directly.
  • Object-oriented: commands (called cmdlets) pass objects, not plain text.

⚠️ Execution Policy (first time only)

By default, Windows blocks script execution. To allow running your own scripts:

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

πŸ“˜ Basic Syntax

  • Cmdlets use Verb-Noun naming: Get-Process # lists running processes Stop-Service name # stops a Windows service
  • Variables start with $: $name = "Manfred"
  • Loops / conditionals are similar to other languages: if ($x -gt 10) { Write-Output "Too big" }
  • Pipelines pass objects, not text: Get-Service | Where-Object { $_.Status -eq "Running" }

πŸƒβ€β™‚οΈ Running a PowerShell Script

  1. Save your script as script.ps1
  2. Open PowerShell (not Command Prompt)
  3. Run the script: .\script.ps1

Choose Y when prompted.


πŸ“„ Simple Script hello.ps1

$name = "World"
Write-Output "Hello, $name!"

Save it, run:

.\hello.ps1
WordPress Appliance - Powered by TurnKey Linux