Almost any repetitive action that you do over and over can be automated. It’s not always the big things that need automation - sometimes all you need is a little helping hand. PowerShell is great for this - here’s an example.

For various reasons, I commonly have the same git repo checked out multiple times in sibling directories - there’s one repo at work that I’ve cloned five separate times. There are times when this works much better than having multiple active development branches in a single working folder.

The major problem with this approach is knowing which directory is being used for which feature. This (abbreviated) directory listing doesn’t really inform, even though it only includes a couple of duplicates:

08:50:21 PS> dir

    Directory: C:\GitHub

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       28/11/2018   6:53 PM                .ps
d-----       28/07/2017   9:03 PM                cmd-colors-solarized
d-----        3/11/2018   6:42 PM                concfg
d-----       13/01/2018   5:26 PM                console
d-----       13/01/2019  11:25 AM                Niche.CommandLineProcessor
d-----       23/12/2019   7:14 PM                Niche.CommandLineProcessor.2
d-----       13/01/2019  11:25 AM                Niche.CommandLineProcessor.3
d-----       21/09/2017   9:36 PM                posh-git

What I need to know is which branch is checked out in each directory. Having to manually cd into each folder to check is relatively time consuming, and its just in the way of getting to work.

It would be really helpful to see which git branch is checked out in each folder. The obvious solution would be to create a helper script, perhaps dir-branches.ps1 that shows a directory listing including branch name.

I took a different route, one that doesn’t require me to remember the name of a custom script.

Did you notice the .ps folder in the above directory listing? That folder contains a script called after-cd.ps1 that’s run automatically when I enter the folder.

How does this work? In the midst of my PowerSell prompt function, I added the following snippet:

if ($global:lastpwd -ne $pwd {
    $global:lastpwd = $pwd
    if (test-path "$pwd/.ps/after-cd.ps1") {
        & "$pwd/.ps/after-cd.ps1"

Whenever I change directory - and the prompt is shown for the first time in the new location - I check to see if there is a script called after-cd.ps1, and if there is, it is run.

In my C:\GitHub folder, the .ps/after-cd.ps1 script queries for all the active git branches and displays a table:

function GetBranch($dir) {
    push-location $dir
    try {
        return git rev-parse --abbrev-ref HEAD
    finally {

$info = get-childitem -Directory -exclude ".ps" |
    format-table -AutoSize @{ 
        Label = "Directory"; Expression = { $_.Name }
    }, @{ 
        Label = "Branch"; Expression = { GetBranch($_) }
    } | Out-String

Write-Host $info.Trim()

When I enter the folder, a listing of the folders and branches is shown automatically:

12:57:38 PS> cd .\GitHub\
Directory                        Branch
---------                        ------
cmd-colors-solarized             master
concfg                           master
console                          feature/powershell-compatibility
Niche.CommandLineProcessor       master
Niche.CommandLineProcessor.2     feature/fluent
Niche.CommandLineProcessor.3     feature/disintegrate-logging
posh-git                         master

12:57:41 PS>

Recognition is much more powerful than memory, so I can immediately move into the folder I want and get to work. This is much faster than trying to remember the right directory, and then having to poke around manually when I choose the wrong one.


blog comments powered by Disqus