Wednesday, July 05, 2017

Using Powershell as a Command / Alias Runner

I've been using an odd little utility(1) for many years to have quick access to commonly run commands, utilities (with parameters), etc.  I finally decided to retire it.  My first thought for a replacement was Unix aliases - I can use the bash shell on windows now.  That went straight to nowheresville; I don't think bash for windows is intended for these kinds of things.  I'm glad it did, however, because I learned how useful powershell is as a command runner.
In order to use it as such, follow these simple steps:
  • At the powershell command prompt, run Test-Path $Profile.  If the result is true, then you have created a profile.  To see the path to your profile, just type $Profile.
  • If the result is false, run this command to create a profile: New-Item -Path $Profile -Type File -Force.  I've no idea how to create it an a location you desire, but this will create a file like this:  "C:\Users\[you]\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1"
  • Open your profile in the Powershell ISE.  Create functions to your heart's desire.  Of course you have access to all of powershell's built in commands.  You can then run your functions from the Powershell command line.  Below are the functions I created.  See how I have a bunch beginning with "csm"?  On the command line I type "csm" followed by ctrl-space and I get a list of all available functions (global functions and my custom functions) beginning with those letters.  Then I can tab through the list to the one I want and hit enter to execute it.  So nice!

# the "." is there for "dot raising" or something.  Doesn't work without it.  I guess we wouldn't know; it's a powershell thing.
function set-config-false {. "C:\Program Files\ConfigUpdater\ConfigUpdater.exe" C:\redacted\Web.config}

# Pass the paths to the files you want to compare.
function compare-files([string]$s1, [string]$s2) {. "C:\Program Files (x86)\WinMerge\WinMergeU.exe" $s1 $s2}

function gulp-compile {. "C:\Data\BatchFiles\gulp.bat"}

function open-git 
    Set-Location C:\Dev\Trebuchet #cd
    . "C:\Program Files\Git\bin\bash.exe"

# Our utilities
function csm {. "C:\redacted.exe"}
function csm-admin {. "C:\redacted.exe" -hiddensettings}
function csm-app-server {. "C:\redacted.exe" -protocol:tcp -port:7999 -connection:"[Common]Demo8"}
function csm-def-editor {. "C:\redacted.exe"}
function csm-restore {. "C:\redacted.exe"}
function csm-server-manager {. "C:\redacted.exe" -a}
function csm-upgrade {. "C:\redacted.exe"}
function reddis-server {. "C:\redacted\redis-server.exe"}

function find-color-under-cursor {. "C:\Program Files\pixie.exe"}
function pixel-ruler {. "C:\Program Files\JRuler.exe"}
function remote {. "C:\WINDOWS\system32\mstsc.exe"}
function show-locked-files {. "C:\Program Files\OpenedFilesView\OpenedFilesView.exe"}
function cleanup-vs {. "C:\Data\BatchFiles\CleanVS.bat"}
function backup-data {. "C:\Data\BatchFiles\backupData.bat"}

    When invoking powershell, type "power" at the windows run box, then ctrl+shift+enter to launch elevated.
    Then, vs will run elevated.
function vs {. "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\devenv.exe"}

1. In fact, I still can't get away from using SlickRun for 2 purposes; an unobtrusive always-on-top clock and a quick-to-invoke paste pad that I'm constantly using, often to simply clean formatting for text.

No comments: