Keith 的个人资料Keith Hill's Blog照片日志列表更多 工具 帮助
7月17日

A Simple Go Function to Simplify Navigating to Popular Directories

Before PowerShell came along I had a number of doskey aliases set up like “gt” to go to the temp dir.  I have since converted those aliases to a single g function (short for “go”):

   1: function g($shortcut) {
   2:     $ht = @{
   3:         bin = "C:\Bin";
   4:         doc = "$([Environment]::GetFolderPath('MyDocuments'))";
   5:         sys = "$env:SystemRoot\System32";
   6:         t   = "$env:Temp";
   7:         win = "$env:SystemRoot";
   8:         cf  = "$env:CommonProgramFiles";
   9:         pf  = "$env:ProgramFiles";
  10:         www = "$env:SystemDrive\inetpub\wwwroot";
  11:         gac = "$env:SystemRoot\Assembly\GAC";
  12:         tfs = "C:\Tfs"
  13:         clr = "$([System.Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory())";
  14:     }
  15:  
  16:     if ($shortcut) {
  17:         if (!$ht["$shortcut"]) { 
  18:             throw "$shortcut isn't valid shortcut. Execute g with no params to see valid shortcuts." 
  19:         }
  20:         cd $ht.$shortcut
  21:     }
  22:     else {
  23:         "Valid shortcuts are:"
  24:         $ht.Keys | Sort | select @{n='Shortcut';e={$_}},@{n='Destination';e={$ht.$_}}
  25:     }
  26: }

 

Usage is pretty simple.  Execute g to see all shortcuts and the associated location:

PS C:\> g
Valid shortcuts are:

Shortcut    Destination
--------   -----------
bin         C:\Bin
cf          C:\Program Files\Common Files
clr         C:\Windows\Microsoft.NET\Framework64\v2.0.50727\
doc         C:\Users\Keith\Documents
gac         C:\Windows\Assembly\GAC
pf          C:\Program Files
sys         C:\Windows\System32
t           C:\Users\Keith\AppData\Local\Temp
tfs         C:\Tfs
win         C:\Windows
www         C:\inetpub\wwwroot

To set-location to the wwwroot dir execute the following:

PS C:\> g www
C:\inetpub\wwwroot

It’s a trivial function but if you spend a lot of time at the PowerShell prompt, it (or something similar) is quite handy.

评论

请稍候...
很抱歉,您输入的评论太长。请缩短您的评论。
您没有输入任何内容,请重试。
很抱歉,我们当前无法添加您的评论。请稍后重试。
若要添加评论,需要您的家长授予您相应权限。请求权限
您的家长禁用了评论功能。
很抱歉,我们当前无法删除您的评论。请稍后重试。
您已超过了一天之内允许提供的评论数上限。请在 24 小时后重试。
因为我们的系统表明您可能在向其他用户提供垃圾评论,您的帐户已禁用了评论功能。如果您认为我们错误地禁用了您的帐户,请联系 Windows Live 支持部门
完成下面的安全检查,您提供评论的过程才能完成。
您在安全检查中键入的字符必须与图片或音频中的字符一致。
HillKeith 在此页禁用了评论功能。

引用通告

引用此项的网络日志