Show node version in PowerShell prompt

I use nvm for Windows so I can quickly used different versions of node.js as my different projects demand. I’ve got burnt a couple of times using the wrong version for a given project though. I figured it would be helpful to always know which version I’m on without having to run node -v so I’ve added it to my shell prompt, which already shows the current time.

PowerShell prompt customized to show the current node.js version

I updated my powershell profile with the customization below. I’m using posh-git so it was pretty easy to do with a helper function.

# Partial content from my profile:
# C:\Users\<user>\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
 
# Print current version of node.js
function Get-NodeVer {
  # This try gracefully handles when node isn't available
  try {
    $ver = node -v
    # Add a space before the node version
    Write-Output ''$ver
  }
  catch {}
}
 
# Add posh-git
Import-Module posh-git
# Customize prompt with the current time and node.js version
$GitPromptSettings.DefaultPromptPrefix.Text = '$(Get-Date -f "H:mm:ss")$(Get-NodeVer) '