New to PowerShell? Jump to: PowerShell in a Nutshell (below).
π Mirroring Android Phone on Windows
β Prerequisite
adb
andscrcpy
must be installed and in yourPATH
(or specify their full paths).
π§ 1. Install adb
(Android Debug Bridge)
Option A: Use Google’s Platform Tools (Recommended)
- Go to the official page:
https://developer.android.com/tools/releases/platform-tools - Download the ZIP for Windows:
- File:
platform-tools-latest-windows.zip
- File:
- Extract the ZIP to a known location, e.g.: makefileCopyEdit
C:\Android\platform-tools
- Add it to your system
PATH
:- Press
Win + S
, search forEnvironment Variables
. - Click “Edit the system environment variables”.
- Click “Environment Variables…”.
- Under System variables, select
Path
, then Edit⦠- Click New, add: makefileCopyEdit
C:\Android\platform-tools
- Click OK on all dialogs.
- Press
- Open a new PowerShell window and test:
adb version
πΊ 2. Install scrcpy
(Android Screen Mirroring)
Option A: Use Prebuilt Windows Release (Recommended)
- Visit the official scrcpy GitHub page:
https://github.com/Genymobile/scrcpy/releases - Download the latest
.zip
file, e.g.:scrcpy-win64-v2.3.1.zip
(or whatever is current) - Extract it to a known location, e.g.:
C:\Android\scrcpy
- Add to
PATH
(same as above), e.g.:C:\Android\scrcpy
- 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
andscrcpy
must be in yourPATH
(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
- Save as
mirror_android.ps1
- Open PowerShell as administrator
- 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
- Save your script as
script.ps1
- Open PowerShell (not Command Prompt)
- Run the script:
.\script.ps1
Choose Y
when prompted.
π Simple Script hello.ps1
$name = "World"
Write-Output "Hello, $name!"
Save it, run:
.\hello.ps1