TIPS & TRICKS

PowerShell Profiles and Add-Path

I often blog about Splunk, but that’s not the only thing that is on my mind. One of the more common things on my mind is PowerShell and how it has affected how I do my work. It’s been hugely impactful. However, it does require a little bit of forethought in terms of setting up your environment. When you first get started with PowerShell, you double-click on the little PS icon and get a perfectly suitable environment for doing basic tasks. However, it can be improved. I used to be a Linux administrator and used the Korn shell for my work. In order to set up my environment, I used a .kshrc file. Similarly, PowerShell has a profile that you can use to customize your environment.

First things first – you need to create a place for your environment. This does it:

PS> mkdir ~\Documents\WindowsPowerShell

Now that you are there, you can edit your profile. You can see what file is going to be edited using:

PS> $profile
C:\Users\Adrian\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

There is a different file for the Integrated Scripting Environment (ISE), so you can have one profile for your PS> prompt and another for the ISE. What you put in there is up to you. One of the things that I do is to set up my development environment. I edit the XML and configuration files with Notepad++ (which you can download for free from their web site). However, that is not added to the PATH by default. I’ve added a short cmdlet for processing the PATH (called Add-Path) and then I use that to alter the path. It’s probably not the best way of doing this (and PowerShell purists can correct me if they like, or point me to http://poshcode.org). But – like many scripting languages – there are many ways of completing the same task, and this is mine. You can also find this function on my Github at https://gist.github.com/adrianhall/956311662fc2a218d9fa

function Add-Path {
  <#
    .SYNOPSIS
      Adds a Directory to the Current Path
    .DESCRIPTION
      Add a directory to the current path.  This is useful for 
      temporary changes to the path or, when run from your 
      profile, for adjusting the path within your powershell 
      prompt.
    .EXAMPLE
      Add-Path -Directory "C:\Program Files\Notepad++"
    .PARAMETER Directory
      The name of the directory to add to the current path.
  #>

  [CmdletBinding()]
  param (
    [Parameter(
      Mandatory=$True,
      ValueFromPipeline=$True,
      ValueFromPipelineByPropertyName=$True,
      HelpMessage='What directory would you like to add?')]
    [Alias('dir')]
    [string[]]$Directory
  )

  PROCESS {
    $Path = $env:PATH.Split(';')

    foreach ($dir in $Directory) {
      if ($Path -contains $dir) {
        Write-Verbose "$dir is already present in PATH"
      } else {
        if (-not (Test-Path $dir)) {
          Write-Verbose "$dir does not exist in the filesystem"
        } else {
          $Path += $dir
        }
      }
    }

    $env:PATH = [String]::Join(';', $Path)
  }
}

Add-Path -Directory “C:\Program Files (x86)\Notepad++”
Set-Alias edit notepad++.exe

Add-Path –Directory “C:\Program Files\Splunk\bin”
Add-Path –Directory “C:\Program Files (x86)\PuTTY”

My profile gives me access to the handy Add-Path cmdlet, adds a few directories to my path to set up ssh (I use PuTTY), Splunk and my editor, and then sets up an alias so I can edit files with an “edit” command. Of course, I do other things in my PowerShell prompt, such as setting up GitHub and remote access privileges for my remote instances, giving me the ability to run “connect <host>” where the host is picked up from a CSV file and transitioned to an IP address – all designed to make my working time as productive as possible. Ultimately, what you put in your profile will depend on how you work and what you need.

Do you have an idea of something you put in your profile, or a favorite tool you can’t do without? Let me know via Twitter at @splunk_ahall.

Splunk
Posted by

Splunk

Join the Discussion