Add a POSH ADB to your Windows Terminal
Windows Terminal is so close to being out of beta. It’s been my default CLI on Windows for about a year. I still don’t think in PowerShell, but I try to use PowerShell as my default shell. I just love how you can configure Terminal.
The default shell for me is PowerShell Core, aka Powershell 7.0. Out of the box, it doesn’t have ADB on the path. When I’m doing Android stuff, I want the ADB. But I don’t want it to be on the path by default. Just like Visual Studio has the “Android ADB Command Prompt” menu option, I wanted to add shell option to Terminals so I can spin up a new PowerShell, but with ADB support. This is what I ended up with:
The first thing I did was to create a PowerShell script that just adds the ADB tooling to the path. My first attempt was:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$NeedsAdb = $true
foreach ($p in $env:Path.Split(";"))
{
if ($p -match "android-sdk\\platform")
{
$NeedsAdb = $false
break
}
}
if ($NeedsAdb)
{
write-host "Adding Android SDK Platform tools to path"
$env:Path += ";C:\Program Files (x86)\Android\android-sdk\platform-tools"
}
It walks through the path and checks to see the folder with the ADB bits is already there. If not, it gets added. That worked but seemed like a lot. I did a quick refactor and got it down to this
1
2
3
4
5
6
7
8
9
10
11
12
13
$NeedsAdb = $true
$env:Path.Split(";") | ForEach-Object {
if ($_ -match "android-sdk\\platform") {
$NeedsAdb = $false
break
}
}
if ($NeedsAdb) {
write-host "Adding Android SDK Platform tools to path"
$env:Path += ";C:\Program Files (x86)\Android\android-sdk\platform-tools"
}
A little cleaner, but still too much. I was taking the path, splitting it up into an array of strings, and then testing each string. But wait, the path is a string. That made it simpler. Instead of doing a string match on each folder the path, I can make one string match against the whole thing.
1
2
3
4
5
if ($env:Path -NotMatch "Android\\android-sdk\\platform")
{
write-host "Adding Android SDK Platform tools to path"
$env:Path += ";${env:ProgramFiles(x86)}\Android\android-sdk\platform-tools"
}
So I called that one add-adb.ps1 and saved it to a common folder that I put scripts in. Next, I wanted a cosmetic tweak so that I knew which shell has the power of ADB. I went to materialdesignicons.com and did a search on “Android”. I found an icon named “android-debug-bridge”, which was perfect. I downloaded the .svg version of the icon and then made a .png file out of it with PhotoShop. I named it android-debug-bridge.png. Then I made a smaller version to be the icon. You can grab the images and the .ps1 file from the following Gist link: https://gist.github.com/anotherlab/364e3805d9ea56b574b394127acc9aa6
Now that I had the script and the images, it’s time to add a new shell profile. From within Windows Terminal, select “Settings” from the drop-down menu. You can also press the CTRL+, shortcut. This will load the settings.json file in the text editor of your choice. I have Visual Studio Code registered as the default app for JSON files. You can use a lesser editor, but that’s on you.
There will be an array of objects named “list”. These objects are the different shells that can be run from within Windows Terminals. I added the following item to the list array
1
2
3
4
5
6
7
8
9
10
11
12
13
14
"list":
[
{
"guid": "{50caca3f-bff1-4891-b7f7-e3a05c040003}",
"fontFace": "Cascadia Code PL",
"backgroundImage": "d:/grfx/android-debug-bridge.png",
"backgroundImageStretchMode": "uniform",
"backgroundImageOpacity": 0.15,
"hidden": false,
"name": "ADB PowerShell",
"icon": "d:/grfx/adb-32.png",
"commandline": "pwsh.exe -noe -c D:/scripts/add-adb.ps1"
},
]
Let’s go over this line by line.
Field | Value |
---|---|
guid | It wants a GUID, so just get one. I used guidgenerator.com, but any GUID generator will do. |
FontFace | I’m using the Powerline version of Cascadia. More on that in a bit |
backgroundImage | Grab it from my Gist or use your own. |
backgroundImageStretchMode | Size the image to fit the Shell window |
backgroundImageOpacity | I set it to be mostly transparent |
hidden | If you want to hide this from the list of shells, just set it to True |
name | Call it what you want |
icon | The tab icon is optional, but you can grab it from my Gist. |
commandline | This is what gets launched. The “-c” option says to run the next parameter and the “-noe” says not to exit after running the command |
And that lets me spin up a new PowerShell with ADB on the path. Mixing PowerShell with ADB makes it easier to do ADB commands that would normally be clunky. For example, I want to test some Android code that would access the photo gallery. When you new up a new Android emulator, there are no images. ADB lets copy files to the emulator’s filesystem, but it doesn’t do wild cards.
You may have notice the “/” slash being used instead of “\” for file paths. Windows Terminal lets you use the “/” as the directory separator and this avoids having to use “\” to get a single “\” in.
I have a folder with a bunch of images that I wanted to copy to an Android Emulator image. I then run the following command from PowerShell:
Get-ChildItem gets a directory listing and I use the -Include parameter to only include the files that have the .jpg and .png extension. If I didn’t need to filter the files list, I could the aliases for Get-ChildItem of gci or ls. I then pipe the results into Foreach-Object. This will execute everything in the {} block for each item. $_.Name is the name of the file and that gets passed to adb push to copy that file to /sdcard/Pictures in the emulator. There’s a bit of typing, but it does save time when trying to copy a set of files over to Android.
About that “Cascadia Code PL” font face. I’m running a theming engine inside PowerShell called “Oh-my-posh”. I’ll do a longer post on it in the future, but the short story is that it makes the PowerShell prompt contain the current git status for the current folder. Read about it and get it here. On the Mac, I use Oh-my-zsh to get a souped-up zsh shell. Scott Hanselman did a good write up of Oh-my-posh here.
Oh-my-posh uses Powerline Glyphs (originally defined here) as part of the status display. So you’ll need a font that includes the Powerline Glyphs. Microsoft’s Cascadia Code font has a version with Powerline and you can get it here. Here’s what that looked like when I was adding the files to the Gist
You can see the color and text information change as I used git to add the icon to the Gist. When working with git, it’s very handy to easily see which branch you are working with and the current status of that branch.
Comments