Jun 14, 2010

Create a persistent alias in PowerShell

Often times, I need to query sharepoint cmdlets as I am starting to use Powershell, normally i wrote something like this:

 get-command where {$_.name -like "get-sp*"}

It works, but would it be nice to have this defined as an alias as I need to run it very often. Sure you can do this by using set-alias cmdlet, but it won;t survive another session, in other words, after you close powershell, it is gone.

the workaround is to define a function or alias in profile:

  •  Open Powershell ISE from Programs>Accessories>PowerShell folder
  •  Run the following code from the immediate window in ISE to create a new profile for all users
if (!(test-path $profile.AllUsersAllHosts)) {new-item -type file -path $profile.AllUsersAllHosts-force}

  •  Run the following code to edit the new profile
           psEdit $profile.AllUsersAllHosts


  •  When profile1.ps1 opens, add the following code:
           function getspcmd { get-command where {$_.name -like "get-sp*"}}
  • save and exit ISE
  • relaunch ISE and type "getspcmd"