Notes/PowerShell

Handy PowerShell Commands

PowerShell is nuts! At the most basic level its your typical shell, but if you dig in further you'll discover that you've got the complete power of .NET Core (or .NET Framework, depending on the version) which allows you to build some insane scripts.

Supporting all that is does however often does translate to more verbositity for certain common commands that you may not necessarily be interested in storing in your profile for later reference (which can be tedious). Which is where this page comes in! Any useful but not essential snippets or plugins I come across will (hopefully) make their way into here over time.

Git

One of the more useful PowerShell plugins is called posh-git which adds some neat enhancements like tab completion and repo status in the location indicator. I highly recommend using it if you use git day-to-day.

Cleanup Merged Branches

If merged branchs aren't closed they will overtime acculate. Cleaning them up takes time, but it doesn't have to take your time. The snippet below will delete any branch that is not merged with master for the remote (-r flag).

git branch -r --merged | `
    ForEach-Object -Process {$_.trim()} | `
    Where-Object {$_ -ne 'master'} | `
    ForEach-Object {$_.Replace('origin/', '')} | `
    ForEach-Object {git push --delete origin $_}

.env

PowerShell itself has no parser for a .env file, however you can get relatively close using ConvertFrom-StringData which even supports comments. The one catch though is that quotes will not be removed for quoted values.

#Content!
title=Adventures in PowerShell
id = 38912
quote="#Content!\ntitle=Adventur..."
# Load
Get-Content "config.env" | ConvertFrom-StringData -OutVariable config | Out-Null

# Output to console
$config

# Load and dump to console
Get-Content "config.env" | ConvertFrom-StringData
Jordan Mele

Jordan Mele

Site owner