More PowerShell Short Scripts

Sometimes it’s the little things…

PowerShell functions are prefect for little repetitive things that we do every few days. One of the things I find myself doing very frequently is creating a directory with the current date while processing daily errors or doing other daily tasks. Sure, this doesn’t take long to do by hand, but it’s also a very quick timesaving function that’s evolved to have a few neat features:

function New-DateDirectory {
    [CmdletBinding(PositionalBinding=$false)]
    param (
        [Parameter()]
        [string]$Format = 'yyyy-MM-dd',
        [Parameter(ValueFromRemainingArguments)]
        [string]$Suffix
      )
    New-Item -ItemType Directory $(@($(Get-Date -Format $Format), $Suffix) -join " ")
}

The default date format can be overridden by specifying a valid parameter to -Format. Any other text provided on the command line will be added to the directory name.

Another timesaver that I’ve adopted more quickly than I expected is a script that finds the URL for the git repository in the current directory and opens it in the default browser. This one might require a bit more explanation:

function Open-GitRepository { 
    git remote -v `
        | Select-String -Pattern '(?:https?://|git@)(.*)\.git \(fetch\)' `
        | ForEach-Object { 
            $url = $_.Matches.Groups[1].Value -replace ':', '/'
            Start-Process "https://$url" 
        } 
}

If the current directory is a Git repository, we can get the URL from which we cloned the repository by typing git remote -v. This command gives us the URLs where we fetch and any that we use to push; a given repository will have one fetch URL (the source of truth) and at least one push URL but can occasionally have multiple. Using this information, we can use Select-String with a handy Regular Expression to match the Git URL (http, https, and SSH URLs in this case) that has (fetch) after it. The result is a single-item list that we can pick apart and turn into a https URL.

So now, after I push a commit, I can easily run Open-GitRepository from the same PowerShell prompt to create my merge request!

2025

Making PowerShell “Posh”

Back in the days when DOS and Windows were separate programs, the default Command Prompt didn’t look exactly the way that it does now. I can’t recall if it w...

Back to Top ↑

2024

PowerShell Short Scripts

As a Data Engineer using Mirth Connect, it’s often tricky to review my team’s merge requests because the merge request document contains the XML representati...

PowerShell Short Scripts

When dealing with Python dependencies, it’s sometimes necessary to determine if a given project is using a given version of a library. Since our team is usin...

Storing dotfiles in GitHub Revisited

A while ago, I wrote about using a bare git repository to manage my dotfiles. That worked somewhat well, but it had some shortcomings. Since, I’ve started us...

Python Context Managers

One of my current projects involves some improvements to code that I wrote last year – specifically enhancing the “Patient Identity Fixer”, which is my pet p...

PowerShell for Data Transformation: JSON

My team at work has a custom FHIR resource that we work with often called an Operation Outcome. We generate one of these for nearly every record we process, ...

Back to Top ↑

2021

Raspberry Pi Zero W “Busy Light”

COVID-19 has us many of us working from home. My husband and I are both fortunate enough to be in this group. However, as a teacher, his schedule is very pre...

Back to Top ↑

2020

Raspberry Pi Setup Script

When I set up a Raspberry Pi, there are a few things that I have to do right out of the gate to make things easier on myself. They involve putting a few file...

Storing dotfiles in GitHub

While I don’t use Linux as my primary operating system, I do use it a lot both at home and at work. It can be frustrating to try to sync up configurations be...

Nitro Cold Brew at Home!

Anyone who knows my husband or me knows that we really like our cold brew, and they also know that we make our own at home. Nitro cold brew has become a bit ...

OpenVPN On Raspberry Pi

So you want to run an OpenVPN Server on a Raspberry Pi? I did too, but it wasn’t easy to figure out the first time. If you want to skip all of this and do it...

Back to Top ↑