Taking Project Inventory with Powershell
Posted on February 21, 2024 by Michael Keane GallowayThe director I work under recently asked my manager, a team lead, and myself for a project inventory. I occasionally eat lunch with this director and a senior engineer on my team. While were having lunch, the inventory came up, and I put forward that we could probably get a lot of the list done by just running a Powershell script on my machine.
After I got back to my desk, I decided to carve out a little bit of time to get the list started with a Powershell one liner. This is what I came up with (line breaks added for readability):
ls **/*
| ? { $_.Mode -eq 'd----' }
| % {
pushd;
cd $_;
(Test-Path ".git") ? $(git remote -v) : "not git repo";
popd
}
| ? { $_.Contains('fetch')}
Taken in order of the command this script does the following:
1. Lists out the contents of all of my directories that I use to organize my git repositories. I typically try to group related projects together, and I don’t have that deep of nesting. If I had deeper nesting, I would probably need to do a depth first search.
2. The ?
is short hand for Where-Object
, so I’m looking for contents of the directories that match the **/*
file pattern that are directories as indicated by the mode d----
.
3. The %
is short hand for the ForEach-Object
command. It’s basically creating a foreach
loop.
4. pushd
puts the current directory on the stack.
5. Since we’ve already filtered for directories we can just cd
into that directory.
6. A ternary operator testing if we are in a git repository by checking for the presence of the .git
directory. If the current directory is a git repository, then get the verbose output of the git remote
command. Otherwise emit "not git repo"
.
7. Finally invoke Where-Object
to extract all of the lines with the term “fetch” present.
This script output about 65 lines containing the git repository fetch URL. I then narrowed that down further since some of those were for previous teams, or projects that the department is no longer responsible. This also ended up under counting some projects that are in other version control services. That said, it got the ball rolling, and we had an initial list within a few minutes.