PoshCode Logo PowerShell Code Repository

TabExpansion (modification of post by foobar view diff)
View followups from foobar | diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1137"></script>download | new post

Ported TabExpansion from V2CTP2 to v1.0 and extended. Please dot souce this script file to use.

  1. Using Ctrl+K, charactors insertion (bihind space or semi-coron or pipe operator) feature : it encloses tokens with parnthesis by default
1 – 1 – 1 – 1<Ctrl+K><tab> 1 – 1 – 1 – (1)<tab> 1 – 1 – 1 (- 1)<tab> 1 – 1 – (1 – 1)<tab><tab><tab>...

with double Ctrl+K, it encloses tokens with $( ) for ( $i=0;$j=1<Ctrl+K><Ctrl+K><tab><tab> for ( $($i=0;$j=1)

if there is a charactor between <Ctrl+K> and <tab>, it is inserted between tokens 1+1 | {$_*2}<Ctrl+K>%<tab> 1+1 | %{$_*2}

1+1 +1+1<Ctrl+K>;<tab> 1+1 ;+1+1

if the charactor is ( or { or {, tokens are enclosed ( ) or { } or {} int<Ctrl+K>[<tab> [int]

with double Ctrl+K, and if there is a charactor between <Ctrl+K> and <tab>, it is inserted between tokens with ‘$’ C:\WINDOWS\system32\notepad.exe<Ctrl+K><Ctrl+K>{<tab> ${C:\WINDOWS\system32\notepad.exe}

% and ? are given special treatment with double Ctrl+K, these enclose tokens with % { } or ? { } behind ‘|’ ls | $_.LastWriteTime -gt “2009/5”<Ctrl+K><Ctrl+K>?<tab> ls | ? {$_.LastWriteTime -gt “2009/5”}

ls | $_.FullName<Ctrl+K><Ctrl+K>%<tab> ls | % {$_.FullName}
  1. ## Tab-Completion
  2. #################
  3. ## Please dot souce this script file.
  4. ## In first loading, it may take a several minutes, in order to generate ProgIDs and TypeNames list.
  5. ##
  6. ## What this can do is:
  7. ##
  8. ## [datetime]::n<tab>
  9. ## [datetime]::now.d<tab>
  10. ## $a = New-Object "Int32[,]" 2,3; $b = "PowerShell","PowerShell"
  11. ## $c = [ref]$a; $d = [ref]$b,$c
  12. ## $d[0].V<tab>[0][0].Get<tab>
  13. ## $d[1].V<tab>[0,0].tos<tab>
  14. ## $function:a<tab>
  15. ## $env:a<tab>
  16. ## [System.Type].a<tab>
  17. ## [datetime].Assembly.a<tab>
  18. ## ).a<tab> # shows System.Type properties and methods...
  19.  
  20. ## #native command name expansion
  21. ## fsu<tab>
  22.  
  23. ## #command option name expansion (for fsutil ipconfig net powershell only)
  24. ## fsutil <tab>
  25. ## ipconfig <tab>
  26. ## net <tab>
  27. ## powershell <tab>
  28.  
  29. ## #TypeNames and Type accelerators expansion
  30. ## [Dec<tab>
  31. ## [system.Man<tab>.auto<tab>.p<tab>
  32. ## New-Object -TypeName IO.Dir<tab>
  33. ## New-Object System.win<tab>.for<tab>.bu<tab>
  34.  
  35. ## #ProgIDs expansion
  36. ## New-Object -Com shel<tab>
  37.  
  38. ## #Enum option expansion
  39. ## Set-ExecutionPolicy <tab>
  40. ## Set-ExecutionPolicy All<tab>
  41. ## Set-ExcusionPolisy -ex <tab>
  42. ## Get-TraceSource Inte<tab>
  43. ## iex -Errora <tab> -wa Sil<tab>
  44.  
  45. ## #WmiClasses expansion
  46. ## Get-WmiObject -class Win32_<tab>
  47. ## gwmi __Instance<tab>
  48.  
  49. ## #Encoding expansion
  50. ## [Out-File | Export-CSV | Select-String | Export-Clixml] -enc <tab>
  51. ## [Add-Content | Get-Content | Set-Content} -Encoding Big<tab>
  52.  
  53. ## #PSProvider name expansion
  54. ## [Get-Location | Get-PSDrive | Get-PSProvider | New-PSDrive | Remove-PSDrive] -PSProvider <tab>
  55. ## Get-PSProvider <tab>
  56. ## pwd -psp al<tab>
  57.  
  58. ## #PSDrive name expansion
  59. ## [Get-PSDrive | New-PSDrive | Remove-PSDrive] [-Name] <tab>
  60. ## Get-PSDrive <tab>
  61. ## pwd -psd <tab>
  62.  
  63. ## #PSSnapin name expansion
  64. ## [Add-PSSnapin | Get-PSSnapin | Remove-PSSnapin ] [-Name] <tab>
  65. ## Get-Command -PSSnapin <tab>
  66. ## Remove-PSSnapin <tab>
  67. ## Get-PSSnapin M<tab>
  68.  
  69. ## #Eventlog name and expansion
  70. ## Get-Eventlog -Log <tab>
  71. ## Get-Eventlog w<tab>
  72.  
  73. ## #Eventlog's entrytype expansion
  74. ## Get-EventLog -EntryType <tab>
  75. ## Get-EventLog -EntryType Er<tab>
  76. ## Get-EventLog -Ent <tab>
  77.  
  78. ## #Service name expansion
  79. ## [Get-Service | Restart-Service | Resume-Service | Start-Service | Stop-Service | Suspend-Service] [-Name] <tab>
  80. ## New-Service -DependsOn <tab>
  81. ## New-Service -Dep e<tab>
  82. ## Get-Service -n <tab>
  83. ## Get-Service <tab>,a<tab>,p<tab>
  84. ## gsv <tab>
  85.  
  86. ## #Service display name expansion
  87. ## [Get-Service | Restart-Service | Resume-Service | Start-Service | Stop-Service | Suspend-Service] [-DisplayName] <tab>
  88. ## Get-Service -Dis <tab>
  89. ## gsv -Dis <tab>,w<tab>,b<tab>
  90.  
  91. ## #Cmdlet and Topic name expansion (this also support default help function and man alias)
  92. ## Get-Help [-Name] about_<tab>
  93. ## Get-Help <tab>
  94.  
  95. ## #Category name expansion (this also support default help function and man alias)
  96. ## Get-Help -Category c<tab>,<tab>
  97.  
  98. ## #Command name expansion
  99. ## Get-Command [-Name] <tab>
  100. ## Get-Command -Name <tab>
  101. ## gcm a<tab>,<tab>
  102.  
  103. ## #Scope expansion
  104. ## [Clear-Variable | Export-Alias | Get-Alias | Get-PSDrive | Get-Variable | Import-Alias
  105. ## New-Alias | New-PSDrive | New-Variable | Remove-Variable | Set-Alias | Set-Variable] -Scope <tab>
  106. ## Clear-Variable -Scope G<tab>
  107. ## Set-Alias  -s <tab>
  108.  
  109. ## #Process name expansion
  110. ## [Get-Process | Stop-Process] [-Name] <tab>
  111. ## Stop-Process -Name <tab>
  112. ## Stop-Process -N pow<tab>
  113. ## Get-Process <tab>
  114. ## ps power<tab>
  115.  
  116. ## #Trace sources expansion
  117. ## [Trace-Command | Get-TraceSource | Set-TraceSource] [-Name] <tab>,a<tab>,p<tab>
  118.  
  119. ## #Trace -ListenerOption expansion
  120. ## [Set-TraceSource | Trace-Command] -ListenerOption <tab>
  121. ## Set-TraceSource -Lis <tab>,n<tab>
  122.  
  123. ## #Trace -Option expansion
  124. ## [Set-TraceSource | Trace-Command] -Option <tab>
  125. ## Set-TraceSource -op <tab>,con<tab>
  126.  
  127. ## #ItemType expansion
  128. ## New-Item -Item <tab>
  129. ## ni -ItemType d<tab>
  130.  
  131. ## #ErrorAction and WarningAction option expansion
  132. ## CMDLET [-ErrorAction | -WarningAction] <tab>
  133. ## CMDLET -Errora s<tab>
  134. ## CMDLET -ea con<tab>
  135. ## CMDLET -wa <tab>
  136.  
  137. ## #Continuous expansion with comma when parameter can treat multiple option
  138. ## # if there are spaces, occur display bug in the line
  139. ## # if strings contains '$' or '-', not work
  140. ## Get-Command -CommandType <tab>,<tab><tab>,cm<tab>
  141. ## pwd -psp <tab>,f<tab>,va<tab>
  142. ## Get-EventLog -EntryType <tab>,i<tab>,s<tab>
  143.  
  144. ## #Enum expansion in method call expression
  145. ## # this needs one or more spaces after left parenthesis or comma
  146. ## $str = "day   night"
  147. ## $str.Split( " ",<space>rem<tab>
  148. ## >>> $str.Split( " ", "RemoveEmptyEntries" ) <Enter> ERROR
  149. ## $str.Split( " ", "RemoveEmptyEntries" -as<space><tab>
  150. ## >>> $str.Split( " ", "RemoveEmptyEntries" -as [System.StringSplitOptions] ) <Enter> Success
  151. ## $type = [System.Type]
  152. ## $type.GetMembers(<space>Def<tab>
  153. ## [IO.Directory]::GetFiles( "C:\", "*",<space>All<tab>
  154. ## # this can do continuous enum expansion with comma and no spaces
  155. ## $type.GetMembers( "IgnoreCase<comma>Dec<tab><comma>In<tab>"
  156. ## [IO.Directory]::GetAccessControl( "C:\",<space>au<tab><comma>ac<tab><comma>G<tab>
  157.  
  158. ## #Better '$_.' expansion when cmdlet output objects or method return objects
  159. ## ls |group { $_.Cr<tab>.Tost<tab>"y")} | tee -var foo| ? { $_.G<tab>.c<tab> -gt 5 } | % { md $_.N<tab> ; copy $_.G<tab> $_.N<tab>  }
  160. ## [IO.DriveInfo]::GetDrives() | ? { $_.A<tab> -gt 1GB }
  161. ## $Host.UI.RawUI.GetBufferContents($rect) | % { $str += $_.c<tab> }
  162. ## gcm Add-Content |select -exp Par<tab>| select -ExpandProperty Par<tab> | ? { $_.Par<tab>.N<tab> -eq "string" }
  163. ## $data = Get-Process
  164. ## $data[2,4,5]  | % { $_.<tab>
  165. ## #when Get-PipeLineObject failed, '$_.' shows methods and properties name of FileInfo and String and Type
  166.  
  167. ## #Property name expansion by -Property parameter
  168. ## [ Format-List | Format-Custom | Format-Table | Format-Wide | Compare-Object |
  169. ##  ConvertTo-Html | Measure-Object | Select-Object | Group-Object | Sort-Object ] [-Property] <tab>
  170. ## Select-Object -ExcludeProperty <tab>
  171. ## Select-Object -ExpandProperty <tab>
  172. ## gcm Get-Acl|select -exp Par<tab>
  173. ## ps |group na<tab>
  174. ## ls | ft A<tab>,M<tab>,L<tab>
  175.  
  176. ## #Hashtable key expansion in the variable name and '.<tab>'
  177. ## Get-Process | Get-Unique | % { $hash += @{$_.ProcessName=$_} }
  178. ## $hash.pow<tab>.pro<tab>
  179.  
  180. ## #Parameter expansion for function, filter and script
  181. ## man -f<tab>
  182. ## 'param([System.StringSplitOptions]$foo,[System.Management.Automation.ActionPreference]$bar,[System.Management.Automation.CommandTypes]$baz) {}' > foobar.ps1
  183. ## .\foobar.ps1 -<tab> -b<tab>
  184.  
  185. ## #Enum expansion for function, filter and scripts
  186. ## # this can do continuous enum expansion with comma and no spaces
  187. ## .\foobar.ps1 -foo rem<tab> -bar <tab><comma>c<tab><comma>sc<tab> -ea silent<tab> -wa con<tab>
  188.  
  189. ## #Enum expansion for assignment expression
  190. ## #needs space(s) after '=' and comma
  191. ## #strongly-typed with -as operator and space(s)
  192. ## $ErrorActionPreference =<space><tab>
  193. ## $cmdtypes = New-Object System.Management.Automation.CommandTypes[] 3
  194. ## $cmdtypes =<space><tab><comma><space>func<tab><comma><space>cmd<tab> -as<space><tab>
  195.  
  196. ## #Path expansion with variable and '\' or '/'
  197. ## $PWD\../../<tab>\<tab>
  198. ## "$env:SystemDrive/pro<tab>/<tab>
  199.  
  200. ## #Operator expansion which starts with '-'
  201. ## "Power","Shell" -m<tab> "Power" -r<tab> '(Pow)(er)','$1d$2'
  202. ## 1..9 -co<tab> 5
  203.  
  204. ## #Keyword expansion
  205. ## fu<tab> test { p<tab> $foo, $bar ) b<tab> "foo" } pr<tab> $_ } en<tab> "$bar" } }
  206.  
  207. ## #Variable name expansion (only global scope)
  208. ## [Clear-Variable | Get-Variable | New-Variable | Remove-Variable | Set-Variable] [-Name] <tab>
  209. ## [Cmdlet | Function | Filter | ExternalScript] -ErrorVariable <tab>
  210. ## [Cmdlet | Function | Filter | ExternalScript] -OutVariable <tab>
  211. ## Tee-Object -Variable <tab>
  212. ##  gv pro<tab>,<tab>
  213. ##  Remove-Variable -Name out<tab>,<tab>,ps<tab>
  214. ##  ... | ... | tee -v <tab>
  215.  
  216. ## #Alias name expansion
  217. ## [Get-Alias | New-Alias | Set-Alias] [-Name] <tab>
  218. ## Export-Alias -Name <tab>
  219. ##  Get-Alias i<tab>,e<tab>,a<tab>
  220. ##  epal -n for<tab>
  221.  
  222. ## #Property name expansion with -groupBy parameter
  223. ## [Format-List | Format-Custom | Format-Table | Format-Wide] -groupBy <tab>
  224. ##  ps | ft -g <tab>
  225. ##  gcm | Format-Wide -GroupBy Par<tab>
  226.  
  227. ## #Type accelerators expansion with no characters
  228. ##  [<tab>
  229. ##  New-Object -typename <tab>
  230. ##  New-Object <tab>
  231.  
  232. ## # File glob expansion with '@'
  233. ##  ls *.txt@<tab>
  234. ##  ls file.txt, foo1.txt, 'bar``[1``].txt', 'foo bar .txt'     # 1 <tab> expanding with comma
  235. ##  ls * -Filter *.txt                                          # 2 <tab> refactoring
  236. ##  ls *.txt                                                    # 3 <tab> (or 1 <tab> & 1 <shift>+<tab>) return original glob pattern
  237.  
  238. ## This can also use '^'(hat) or '~'(tilde) for Excluding
  239. ##  ls <hat>*.txt@<tab>
  240. ##  ls foo.ps1, 'bar``[1``].xml'                # 1 <tab> expanding with comma
  241. ##  ls * -Filter * -Excluding *.txt             # 2 <tab> refactoring
  242. ##  *.txt<tilde>foo*<tilde>bar*@<tab>
  243. ##  ls file.txt                                 # 1 <tab> expanding with comma
  244. ##  ls * -Filter *.txt -Excluding foo*, bar*    # 2 <tab> refactoring
  245.  
  246. ## # Ported history expansion from V2CTP3 TabExpansion with '#' ( #<pattern> or #<id> )
  247. ##  ls * -Filter * -Excluding foo*, bar*<Enter>
  248. ##  #ls<tab>
  249. ##  #1<tab>
  250.  
  251. ## # Command buffer stack with ';'(semicolon)
  252. ##  ls * -Filter * -Excluding foo*, bar*<semicolon><tab> # push command1
  253. ##  echo "PowerShell"<semicolon><tab> # push command2
  254. ##  get-process<semicolon><tab> # push command3
  255. ##  {COMMAND}<Enter> # execute another command
  256. ##  get-process # Auto pop command3 from stack by LIFO
  257. ## This can also hand-operated pop with ';,'(semicolon&comma) or ';:'(semicolon&colon)
  258. ##  get-process; <semicolon><comma><tab>
  259. ##  get-process; echo "PowerShell" # pop command2 from stack by LIFO
  260.  
  261. ## # Function name expansion after 'function' or 'filter' keywords
  262. ## function cl<tab>
  263.  
  264. ## #Switch syntax option expansion
  265. ##  switch -w<tab> -f<tab>
  266.  
  267. ## #Better powershell.exe option expansion with '-'
  268. ##  powershell -no<tab> -<tab> -en<tab>
  269.  
  270. ## #A part of PowerShell attributes expansion ( CmdletBinding, Parameter, Alias, Validate*, Allow* )
  271. ##  [par<tab>
  272. ##  [cmd<tab>
  273.  
  274. ## #Member expansion for CmdletBinding and Parameter attributes
  275. ##  [Parameter(man<tab>,<tab>1,val<tab>$true)]
  276. ##  [CmdletBinding( <tab>"foo", su<tab>$true)]
  277.  
  278. ## #Several current date/time formats with Ctrl+D
  279. ##  <Ctrl+D><tab><tab><tab><tab><tab>...
  280.  
  281. ## #Hand-operated pop from command buffer with Ctrl+P (this is also available with ';:' or ';,')
  282. ##  <command>;<tab> # push command
  283. ##  <Ctrl+D><tab> # pop
  284.  
  285. ## #Paste clipboard with Ctrl+V
  286. ##  <Ctrl+V><tab>
  287.  
  288. ## # Cut current line with Ctrl+X
  289. ##  <command><Ctrl+X><tab>
  290.  
  291. ## # Cut words with a character after Ctrl+X until the character
  292. ## 1: PS > dir -Filter *.txt<Ctrl+X>-<tab> # Cut words until '-'
  293. ## 2: PS > dir -
  294. ## 3: PS > dir -<Ctrl+V><tab> # Paste words that were copyed now
  295.  
  296. ## # Cut last word in current line with Ctrl+Z
  297. ## 1: PS >  Get-ChildItem *.txt<Ctrl+Z><tab> # Cut last word in current line
  298. ## 2: PS >  Get-ChildItem
  299. ## 3: PS >  Get-ChildItem -Exclude <Ctrl+V><tab> # Paste last word that was copyed now
  300.  
  301. ## #[ScriptBlock] member expansion for [ScriptBlock] literal
  302. ##  { 1+1 }.inv<tab>
  303.  
  304. ## #A part of history commands expansion with Ctrl+L (using split(";"),split("|"),split(";|") )
  305. ## ls -Force -Recurse -Filter *.txt | ? { $_.LastWriteTime -lt  [DateTime]::Today } ; echo PowerShell
  306. ## ls<Ctrl+L><tab><tab>
  307. ## ?<Ctrl+L><tab><tab>
  308. ## ec<Ctrl+L><tab>
  309.  
  310. ## # Using Ctrl+K, characters insertion (bihind space or semi-coron or pipe operator) feature : it encloses tokens with parnthesis by default
  311. ##  1 - 1 - 1 - 1<Ctrl+K><tab>
  312. ##  1 - 1 - 1 - (1)<tab>
  313. ##  1 - 1 - 1 (- 1)<tab>
  314. ##  1 - 1 - (1 - 1)<tab><tab><tab>...
  315. ## with double Ctrl+K, it encloses tokens with $( )
  316. ##  for ( $i=0;$j=1<Ctrl+K><Ctrl+K><tab><tab>
  317. ##  for ( $($i=0;$j=1)
  318. ## if there is a character between <Ctrl+K> and  <tab>, it is inserted between tokens
  319. ##  1+1 | {$_*2}<Ctrl+K>%<tab>
  320. ##  1+1 | %{$_*2}
  321. ##  1+1 +1+1<Ctrl+K>;<tab>
  322. ##  1+1 ;+1+1
  323. ## if the character is ( or { or {, tokens are enclosed ( ) or { } or {}
  324. ##  int<Ctrl+K>[<tab>
  325. ##  [int]
  326. ## with double Ctrl+K, and if there is a character between <Ctrl+K> and  <tab>, it is inserted between tokens with '$'
  327. ##  C:\WINDOWS\system32\notepad.exe<Ctrl+K><Ctrl+K>{<tab>
  328. ##  ${C:\WINDOWS\system32\notepad.exe}
  329. ## % and ? are given special treatment with double Ctrl+K, these enclose tokens with % { } or ? { } behind '|'
  330. ##  ls | $_.LastWriteTime -gt "2009/5"<Ctrl+K><Ctrl+K>?<tab>
  331. ##  ls | ? {$_.LastWriteTime -gt "2009/5"}
  332. ##  ls | $_.FullName<Ctrl+K><Ctrl+K>%<tab>
  333. ##  ls | % {$_.FullName}
  334.  
  335. ### Generate ProgIDs list...
  336. if ( Test-Path $PSHOME\ProgIDs.txt )
  337. {
  338.     $_ProgID = type $PSHOME\ProgIDs.txt -ReadCount 0
  339. }
  340. else
  341. {
  342.     $_HKCR = [Microsoft.Win32.Registry]::ClassesRoot.OpenSubKey("CLSID\")
  343.     $_ProgID = New-Object ( [System.Collections.Generic.List``1].MakeGenericType([String]) )
  344.     foreach ( $_subkey in $_HKCR.GetSubKeyNames() )
  345.     {
  346.         foreach ( $_i in [Microsoft.Win32.Registry]::ClassesRoot.OpenSubKey("CLSID\$_subkey\ProgID") )
  347.         {
  348.             if ($_i -ne $null)
  349.             {
  350.                 $_ProgID.Add($_i.GetValue(""))
  351.             }
  352.         }
  353.     }
  354.     '$_ProgID was updated...' | Out-Host
  355.     $_ProgID = $_ProgID|sort -Unique
  356.  
  357.     Set-Content -Value $_ProgID -Path $PSHOME\ProgIDs.txt -Verbose
  358. }
  359.  
  360. ### Generate TypeNames list...
  361.  
  362. if ( Test-Path $PSHOME\TypeNames.txt )
  363. {
  364.     $_TypeNames = type $PSHOME\TypeNames.txt -ReadCount 0
  365. }
  366. else
  367. {
  368.     $_TypeNames = New-Object ( [System.Collections.Generic.List``1].MakeGenericType([String]) )
  369.     foreach ( $_asm in [AppDomain]::CurrentDomain.GetAssemblies() )
  370.     {
  371.         foreach ( $_type in $_asm.GetTypes() )
  372.         {
  373.             $_TypeNames.Add($_type.FullName)
  374.         }
  375.     }
  376.     '$_TypeNames was updated...' | Out-Host
  377.     $_TypeNames = $_TypeNames | sort -Unique
  378.  
  379.     Set-Content -Value $_TypeNames -Path $PSHOME\TypeNames.txt -Verbose
  380. }
  381.  
  382. if ( Test-Path $PSHOME\TypeNames_System.txt )
  383. {
  384.     $_TypeNames_System = type $PSHOME\TypeNames_System.txt -ReadCount 0
  385. }
  386. else
  387. {
  388.     $_TypeNames_System = $_TypeNames -like "System.*" -replace '^System\.'
  389.     '$_TypeNames_System was updated...' | Out-Host
  390.     Set-Content -Value $_TypeNames_System -Path $PSHOME\TypeNames_System.txt -Verbose
  391. }
  392.  
  393. ### Generate WMIClasses list...
  394. if ( Test-Path $PSHOME\WMIClasses.txt )
  395. {
  396.     $_WMIClasses = type $PSHOME\WMIClasses.txt -ReadCount 0
  397. }
  398. else
  399. {
  400.     $_WMIClasses = New-Object ( [System.Collections.Generic.List``1].MakeGenericType([String]) )
  401.     foreach ( $_class in gwmi -List )
  402.     {
  403.         $_WMIClasses.Add($_class.Name)
  404.     }
  405.     $_WMIClasses = $_WMIClasses | sort -Unique
  406.     '$_WMIClasses was updated...' | Out-Host
  407.     Set-Content -Value $_WMIClasses -Path $PSHOME\WMIClasses.txt -Verbose
  408. }
  409.  
  410. [Reflection.Assembly]::LoadWithPartialName( "System.Windows.Forms" ) | Out-Null
  411. $global:_cmdstack = New-Object Collections.Stack
  412. $global:_snapin = $null
  413. $global:_TypeAccelerators = "ADSI", "Array", "Bool", "Byte", "Char", "Decimal", "Double", "float", "hashtable", "int", "Long", "PSObject", "ref",
  414.                             "Regex", "ScriptBlock", "Single", "String", "switch", "Type", "WMI", "WMIClass", "WMISearcher", "xml"
  415. $global:_cmdline = New-Object Collections.ArrayList
  416.  
  417. iex (@'
  418. function prompt {
  419. h -Count 1 -OutVariable line |
  420. % { $_.CommandLine.Split("|"); $_.CommandLine.Split(";"); $_.CommandLine.Split(";"); $_.CommandLine.Split(";|") } |
  421. Get-Unique | ? { $_ -ne $line.CommandLine -and $_ -notmatch '^\s*$' } |
  422. % { $_.Trim(" ") } |
  423. ? { $global:_cmdline -notcontains $_ } | % { Set-CommandLine $_ | Out-Null }
  424. if ($_cmdstack.Count -gt 0) {
  425.    $line = $global:_cmdstack.Pop() -replace '([[\]\(\)+{}?~%])','{$1}'
  426.    [System.Windows.Forms.SendKeys]::SendWait($line)
  427. }
  428. '@ + @"
  429. ${function:prompt}
  430. }
  431. "@)
  432.  
  433. function Write-ClassNames ( $data, $i, $prefix='', $sep='.' )
  434. {
  435.     $preItem = ""
  436.     foreach ( $class in $data -like $_opt )
  437.     {
  438.         $Item = $class.Split($sep)
  439.         if ( $preItem -ne $Item[$i] )
  440.         {
  441.             if ( $i+1 -eq $Item.Count )
  442.             {
  443.                 if ( $prefix -eq "[" )
  444.                 {
  445.                     $suffix = "]"
  446.                 }
  447.                 elseif ( $sep -eq "_" )
  448.                 {
  449.                     $suffix = ""
  450.                 }
  451.                 else
  452.                 {
  453.                     $suffix = " "
  454.                 }
  455.             }
  456.             else
  457.             {
  458.                 $suffix = ""
  459.             }
  460.             $prefix + $_opt.Substring(0, $_opt.LastIndexOf($sep)+1) + $Item[$i] + $suffix
  461.  
  462.             $preItem = $Item[$i]
  463.         }
  464.     }
  465. }
  466.  
  467. function Get-PipeLineObject {
  468.  
  469.     $i = -2
  470.     $property = $null
  471.     do {
  472.         $str = $line.Split("|")
  473.         # extract the command name from the string
  474.         # first split the string into statements and pipeline elements
  475.         # This doesn't handle strings however.
  476.         $_cmdlet = [regex]::Split($str[$i], '[|;=]')[-1]
  477.  
  478.         # take the first space separated token of the remaining string
  479.         # as the command to look up. Trim any leading or trailing spaces
  480.         # so you don't get leading empty elements.
  481.         $_cmdlet = $_cmdlet.Trim().Split()[0]
  482.  
  483.         if ( $_cmdlet -eq "?" )
  484.         {
  485.             $_cmdlet = "Where-Object"
  486.         }
  487.  
  488.         $global:_exp = $_cmdlet
  489.  
  490.         # now get the info object for it...
  491.         $_cmdlet = @(Get-Command -type 'cmdlet,alias' $_cmdlet)[0]
  492.  
  493.         # loop resolving aliases...
  494.         while ($_cmdlet.CommandType -eq 'alias')
  495.         {
  496.             $_cmdlet = @(Get-Command -type 'cmdlet,alias' $_cmdlet.Definition)[0]
  497.         }
  498.  
  499.         if ( "Select-Object" -eq $_cmdlet )
  500.         {
  501.             if ( $str[$i] -match '\s+-Exp\w*[\s:]+(\w+)' )
  502.             {
  503.                 $property = $Matches[1] + ";" + $property
  504.             }
  505.         }
  506.  
  507.         $i--
  508.     } while ( "Get-Unique", "Select-Object", "Sort-Object", "Tee-Object", "Where-Object" -contains $_cmdlet )
  509.  
  510.     if ( $global:_forgci -eq $null )
  511.     {
  512.         $a = @(ls "Alias:\")[0]
  513.         $e = @(ls "Env:\")[0]
  514.         $f = @(ls "Function:\")[0]
  515.         $h = @(ls "HKCU:\")[0]
  516.         $v = @(ls "Variable:\")[0]
  517.         $c = @(ls "cert:\")[0]
  518.         $global:_forgci = gi $PSHOME\powershell.exe |
  519.         Add-Member -Name CommandType -MemberType 'NoteProperty' -Value $f.CommandType -PassThru |
  520.         Add-Member -Name Definition -MemberType 'NoteProperty' -Value $a.Definition -PassThru |
  521.         Add-Member -Name Description -MemberType 'NoteProperty' -Value $a.Description -PassThru |
  522.         Add-Member -Name Key -MemberType 'NoteProperty' -Value $e.Key -PassThru |
  523.         Add-Member -Name Location -MemberType 'NoteProperty' -Value $c.Location -PassThru |
  524.         Add-Member -Name LocationName -MemberType 'NoteProperty' -Value $c.LocationName -PassThru |
  525.         Add-Member -Name Options -MemberType 'NoteProperty' -Value $a.Options -PassThru |
  526.         Add-Member -Name ReferencedCommand -MemberType 'NoteProperty' -Value $a.ReferencedCommand -PassThru |
  527.         Add-Member -Name ResolvedCommand -MemberType 'NoteProperty' -Value $a.ResolvedCommand -PassThru |
  528.         Add-Member -Name ScriptBlock -MemberType 'NoteProperty' -Value $f.ScriptBlock -PassThru |
  529.         Add-Member -Name StoreNames -MemberType 'NoteProperty' -Value $c.StoreNames -PassThru |
  530.         Add-Member -Name SubKeyCount -MemberType 'NoteProperty' -Value $h.SubKeyCount -PassThru |
  531.         Add-Member -Name Value -MemberType 'NoteProperty' -Value $e.Value -PassThru |
  532.         Add-Member -Name ValueCount -MemberType 'NoteProperty' -Value $h.ValueCount -PassThru |
  533.         Add-Member -Name Visibility -MemberType 'NoteProperty' -Value $a.Visibility -PassThru |
  534.         Add-Member -Name Property -MemberType 'NoteProperty' -Value $h.Property -PassThru |
  535.         Add-Member -Name ResolvedCommandName -MemberType 'NoteProperty' -Value $a.ResolvedCommandName -PassThru |
  536.         Add-Member -Name Close -MemberType 'ScriptMethod' -Value {} -PassThru |
  537.         Add-Member -Name CreateSubKey -MemberType 'ScriptMethod' -Value {} -PassThru |
  538.         Add-Member -Name DeleteSubKey -MemberType 'ScriptMethod' -Value {} -PassThru |
  539.         Add-Member -Name DeleteSubKeyTree -MemberType 'ScriptMethod' -Value {} -PassThru |
  540.         Add-Member -Name DeleteValue -MemberType 'ScriptMethod' -Value {} -PassThru |
  541.         Add-Member -Name Flush -MemberType 'ScriptMethod' -Value {} -PassThru |
  542.         Add-Member -Name GetSubKeyNames -MemberType 'ScriptMethod' -Value {} -PassThru |
  543.         Add-Member -Name GetValue -MemberType 'ScriptMethod' -Value {} -PassThru |
  544.         Add-Member -Name GetValueKind -MemberType 'ScriptMethod' -Value {} -PassThru |
  545.         Add-Member -Name GetValueNames -MemberType 'ScriptMethod' -Value {} -PassThru |
  546.         Add-Member -Name IsValidValue -MemberType 'ScriptMethod' -Value {} -PassThru |
  547.         Add-Member -Name OpenSubKey -MemberType 'ScriptMethod' -Value {} -PassThru |
  548.         Add-Member -Name SetValue -MemberType 'ScriptMethod' -Value {} -PassThru
  549.     }
  550.  
  551.     if ( $global:_mix -eq $null )
  552.     {
  553.         $f = gi $PSHOME\powershell.exe
  554.         $t = [type]
  555.         $s = ""
  556.         $global:_mix = `
  557.         Add-Member -InputObject (New-Object PSObject) -Name Mode -MemberType 'NoteProperty' -Value $f.Mode -PassThru |
  558.         Add-Member -Name Assembly -MemberType 'NoteProperty' -Value $t.Assembly -PassThru |
  559.         Add-Member -Name AssemblyQualifiedName -MemberType 'NoteProperty' -Value $t.AssemblyQualifiedName -PassThru |
  560.         Add-Member -Name Attributes -MemberType 'NoteProperty' -Value $f.Attributes -PassThru |
  561.         Add-Member -Name BaseType -MemberType 'NoteProperty' -Value $t.BaseType -PassThru |
  562.         Add-Member -Name ContainsGenericParameters -MemberType 'NoteProperty' -Value $t.ContainsGenericParameters -PassThru |
  563.         Add-Member -Name CreationTime -MemberType 'NoteProperty' -Value $f.CreationTime -PassThru |
  564.         Add-Member -Name CreationTimeUtc -MemberType 'NoteProperty' -Value $f.CreationTimeUtc -PassThru |
  565.         Add-Member -Name DeclaringMethod -MemberType 'NoteProperty' -Value $t.DeclaringMethod -PassThru |
  566.         Add-Member -Name DeclaringType -MemberType 'NoteProperty' -Value $t.DeclaringType -PassThru |
  567.         Add-Member -Name Exists -MemberType 'NoteProperty' -Value $f.Exists -PassThru |
  568.         Add-Member -Name Extension -MemberType 'NoteProperty' -Value $f.Extension -PassThru |
  569.         Add-Member -Name FullName -MemberType 'NoteProperty' -Value $f.FullName -PassThru |
  570.         Add-Member -Name GenericParameterAttributes -MemberType 'NoteProperty' -Value $t.GenericParameterAttributes -PassThru |
  571.         Add-Member -Name GenericParameterPosition -MemberType 'NoteProperty' -Value $t.GenericParameterPosition -PassThru |
  572.         Add-Member -Name GUID -MemberType 'NoteProperty' -Value $t.GUID -PassThru |
  573.         Add-Member -Name HasElementType -MemberType 'NoteProperty' -Value $t.HasElementType -PassThru |
  574.         Add-Member -Name IsAbstract -MemberType 'NoteProperty' -Value $t.IsAbstract -PassThru |
  575.         Add-Member -Name IsAnsiClass -MemberType 'NoteProperty' -Value $t.IsAnsiClass -PassThru |
  576.         Add-Member -Name IsArray -MemberType 'NoteProperty' -Value $t.IsArray -PassThru |
  577.         Add-Member -Name IsAutoClass -MemberType 'NoteProperty' -Value $t.IsAutoClass -PassThru |
  578.         Add-Member -Name IsAutoLayout -MemberType 'NoteProperty' -Value $t.IsAutoLayout -PassThru |
  579.         Add-Member -Name IsByRef -MemberType 'NoteProperty' -Value $t.IsByRef -PassThru |
  580.         Add-Member -Name IsClass -MemberType 'NoteProperty' -Value $t.IsClass -PassThru |
  581.         Add-Member -Name IsCOMObject -MemberType 'NoteProperty' -Value $t.IsCOMObject -PassThru |
  582.         Add-Member -Name IsContextful -MemberType 'NoteProperty' -Value $t.IsContextful -PassThru |
  583.         Add-Member -Name IsEnum -MemberType 'NoteProperty' -Value $t.IsEnum -PassThru |
  584.         Add-Member -Name IsExplicitLayout -MemberType 'NoteProperty' -Value $t.IsExplicitLayout -PassThru |
  585.         Add-Member -Name IsGenericParameter -MemberType 'NoteProperty' -Value $t.IsGenericParameter -PassThru |
  586.         Add-Member -Name IsGenericType -MemberType 'NoteProperty' -Value $t.IsGenericType -PassThru |
  587.         Add-Member -Name IsGenericTypeDefinition -MemberType 'NoteProperty' -Value $t.IsGenericTypeDefinition -PassThru |
  588.         Add-Member -Name IsImport -MemberType 'NoteProperty' -Value $t.IsImport -PassThru |
  589.         Add-Member -Name IsInterface -MemberType 'NoteProperty' -Value $t.IsInterface -PassThru |
  590.         Add-Member -Name IsLayoutSequential -MemberType 'NoteProperty' -Value $t.IsLayoutSequential -PassThru |
  591.         Add-Member -Name IsMarshalByRef -MemberType 'NoteProperty' -Value $t.IsMarshalByRef -PassThru |
  592.         Add-Member -Name IsNested -MemberType 'NoteProperty' -Value $t.IsNested -PassThru |
  593.         Add-Member -Name IsNestedAssembly -MemberType 'NoteProperty' -Value $t.IsNestedAssembly -PassThru |
  594.         Add-Member -Name IsNestedFamANDAssem -MemberType 'NoteProperty' -Value $t.IsNestedFamANDAssem -PassThru |
  595.         Add-Member -Name IsNestedFamily -MemberType 'NoteProperty' -Value $t.IsNestedFamily -PassThru |
  596.         Add-Member -Name IsNestedFamORAssem -MemberType 'NoteProperty' -Value $t.IsNestedFamORAssem -PassThru |
  597.         Add-Member -Name IsNestedPrivate -MemberType 'NoteProperty' -Value $t.IsNestedPrivate -PassThru |
  598.         Add-Member -Name IsNestedPublic -MemberType 'NoteProperty' -Value $t.IsNestedPublic -PassThru |
  599.         Add-Member -Name IsNotPublic -MemberType 'NoteProperty' -Value $t.IsNotPublic -PassThru |
  600.         Add-Member -Name IsPointer -MemberType 'NoteProperty' -Value $t.IsPointer -PassThru |
  601.         Add-Member -Name IsPrimitive -MemberType 'NoteProperty' -Value $t.IsPrimitive -PassThru |
  602.         Add-Member -Name IsPublic -MemberType 'NoteProperty' -Value $t.IsPublic -PassThru |
  603.         Add-Member -Name IsSealed -MemberType 'NoteProperty' -Value $t.IsSealed -PassThru |
  604.         Add-Member -Name IsSerializable -MemberType 'NoteProperty' -Value $t.IsSerializable -PassThru |
  605.         Add-Member -Name IsSpecialName -MemberType 'NoteProperty' -Value $t.IsSpecialName -PassThru |
  606.         Add-Member -Name IsUnicodeClass -MemberType 'NoteProperty' -Value $t.IsUnicodeClass -PassThru |
  607.         Add-Member -Name IsValueType -MemberType 'NoteProperty' -Value $t.IsValueType -PassThru |
  608.         Add-Member -Name IsVisible -MemberType 'NoteProperty' -Value $t.IsVisible -PassThru |
  609.         Add-Member -Name LastAccessTime -MemberType 'NoteProperty' -Value $f.LastAccessTime -PassThru |
  610.         Add-Member -Name LastAccessTimeUtc -MemberType 'NoteProperty' -Value $f.LastAccessTimeUtc -PassThru |
  611.         Add-Member -Name LastWriteTime -MemberType 'NoteProperty' -Value $f.LastWriteTime -PassThru |
  612.         Add-Member -Name LastWriteTimeUtc -MemberType 'NoteProperty' -Value $f.LastWriteTimeUtc -PassThru |
  613.         Add-Member -Name MemberType -MemberType 'NoteProperty' -Value $t.MemberType -PassThru |
  614.         Add-Member -Name MetadataToken -MemberType 'NoteProperty' -Value $t.MetadataToken -PassThru |
  615.         Add-Member -Name Module -MemberType 'NoteProperty' -Value $t.Module -PassThru |
  616.         Add-Member -Name Name -MemberType 'NoteProperty' -Value $t.Name -PassThru |
  617.         Add-Member -Name Namespace -MemberType 'NoteProperty' -Value $t.Namespace -PassThru |
  618.         Add-Member -Name Parent -MemberType 'NoteProperty' -Value $f.Parent -PassThru |
  619.         Add-Member -Name ReflectedType -MemberType 'NoteProperty' -Value $t.ReflectedType -PassThru |
  620.         Add-Member -Name Root -MemberType 'NoteProperty' -Value $f.Root -PassThru |
  621.         Add-Member -Name StructLayoutAttribute -MemberType 'NoteProperty' -Value $t.StructLayoutAttribute -PassThru |
  622.         Add-Member -Name TypeHandle -MemberType 'NoteProperty' -Value $t.TypeHandle -PassThru |
  623.         Add-Member -Name TypeInitializer -MemberType 'NoteProperty' -Value $t.TypeInitializer -PassThru |
  624.         Add-Member -Name UnderlyingSystemType -MemberType 'NoteProperty' -Value $t.UnderlyingSystemType -PassThru |
  625.         Add-Member -Name PSChildName -MemberType 'NoteProperty' -Value $f.PSChildName -PassThru |
  626.         Add-Member -Name PSDrive -MemberType 'NoteProperty' -Value $f.PSDrive -PassThru |
  627.         Add-Member -Name PSIsContainer -MemberType 'NoteProperty' -Value $f.PSIsContainer -PassThru |
  628.         Add-Member -Name PSParentPath -MemberType 'NoteProperty' -Value $f.PSParentPath -PassThru |
  629.         Add-Member -Name PSPath -MemberType 'NoteProperty' -Value $f.PSPath -PassThru |
  630.         Add-Member -Name PSProvider -MemberType 'NoteProperty' -Value $f.PSProvider -PassThru |
  631.         Add-Member -Name BaseName -MemberType 'NoteProperty' -Value $f.BaseName -PassThru |
  632.         Add-Member -Name Clone -MemberType 'ScriptMethod' -Value {} -PassThru |
  633.         Add-Member -Name CompareTo -MemberType 'ScriptMethod' -Value {} -PassThru |
  634.         Add-Member -Name Contains -MemberType 'ScriptMethod' -Value {} -PassThru |
  635.         Add-Member -Name CopyTo -MemberType 'ScriptMethod' -Value {} -PassThru |
  636.         Add-Member -Name Create -MemberType 'ScriptMethod' -Value {} -PassThru |
  637.         Add-Member -Name CreateObjRef -MemberType 'ScriptMethod' -Value {} -PassThru |
  638.         Add-Member -Name CreateSubdirectory -MemberType 'ScriptMethod' -Value {} -PassThru |
  639.         Add-Member -Name Delete -MemberType 'ScriptMethod' -Value {} -PassThru |
  640.         Add-Member -Name EndsWith -MemberType 'ScriptMethod' -Value {} -PassThru |
  641.         Add-Member -Name FindInterfaces -MemberType 'ScriptMethod' -Value {} -PassThru |
  642.         Add-Member -Name FindMembers -MemberType 'ScriptMethod' -Value {} -PassThru |
  643.         Add-Member -Name GetAccessControl -MemberType 'ScriptMethod' -Value {} -PassThru |
  644.         Add-Member -Name GetArrayRank -MemberType 'ScriptMethod' -Value {} -PassThru |
  645.         Add-Member -Name GetConstructor -MemberType 'ScriptMethod' -Value {} -PassThru |
  646.         Add-Member -Name GetConstructors -MemberType 'ScriptMethod' -Value {} -PassThru |
  647.         Add-Member -Name GetCustomAttributes -MemberType 'ScriptMethod' -Value {} -PassThru |
  648.         Add-Member -Name GetDefaultMembers -MemberType 'ScriptMethod' -Value {} -PassThru |
  649.         Add-Member -Name GetDirectories -MemberType 'ScriptMethod' -Value {} -PassThru |
  650.         Add-Member -Name GetElementType -MemberType 'ScriptMethod' -Value {} -PassThru |
  651.         Add-Member -Name GetEnumerator -MemberType 'ScriptMethod' -Value {} -PassThru |
  652.         Add-Member -Name GetEvent -MemberType 'ScriptMethod' -Value {} -PassThru |
  653.         Add-Member -Name GetEvents -MemberType 'ScriptMethod' -Value {} -PassThru |
  654.         Add-Member -Name GetField -MemberType 'ScriptMethod' -Value {} -PassThru |
  655.         Add-Member -Name GetFields -MemberType 'ScriptMethod' -Value {} -PassThru |
  656.         Add-Member -Name GetFiles -MemberType 'ScriptMethod' -Value {} -PassThru |
  657.         Add-Member -Name GetFileSystemInfos -MemberType 'ScriptMethod' -Value {} -PassThru |
  658.         Add-Member -Name GetGenericArguments -MemberType 'ScriptMethod' -Value {} -PassThru |
  659.         Add-Member -Name GetGenericParameterConstraints -MemberType 'ScriptMethod' -Value {} -PassThru |
  660.         Add-Member -Name GetGenericTypeDefinition -MemberType 'ScriptMethod' -Value {} -PassThru |
  661.         Add-Member -Name GetInterface -MemberType 'ScriptMethod' -Value {} -PassThru |
  662.         Add-Member -Name GetInterfaceMap -MemberType 'ScriptMethod' -Value {} -PassThru |
  663.         Add-Member -Name GetInterfaces -MemberType 'ScriptMethod' -Value {} -PassThru |
  664.         Add-Member -Name GetLifetimeService -MemberType 'ScriptMethod' -Value {} -PassThru |
  665.         Add-Member -Name GetMember -MemberType 'ScriptMethod' -Value {} -PassThru |
  666.         Add-Member -Name GetMembers -MemberType 'ScriptMethod' -Value {} -PassThru |
  667.         Add-Member -Name GetMethod -MemberType 'ScriptMethod' -Value {} -PassThru |
  668.         Add-Member -Name GetMethods -MemberType 'ScriptMethod' -Value {} -PassThru |
  669.         Add-Member -Name GetNestedType -MemberType 'ScriptMethod' -Value {} -PassThru |
  670.         Add-Member -Name GetNestedTypes -MemberType 'ScriptMethod' -Value {} -PassThru |
  671.         Add-Member -Name GetObjectData -MemberType 'ScriptMethod' -Value {} -PassThru |
  672.         Add-Member -Name GetProperties -MemberType 'ScriptMethod' -Value {} -PassThru |
  673.         Add-Member -Name GetProperty -MemberType 'ScriptMethod' -Value {} -PassThru |
  674.         Add-Member -Name GetTypeCode -MemberType 'ScriptMethod' -Value {} -PassThru |
  675.         Add-Member -Name IndexOf -MemberType 'ScriptMethod' -Value {} -PassThru |
  676.         Add-Member -Name IndexOfAny -MemberType 'ScriptMethod' -Value {} -PassThru |
  677.         Add-Member -Name InitializeLifetimeService -MemberType 'ScriptMethod' -Value {} -PassThru |
  678.         Add-Member -Name Insert -MemberType 'ScriptMethod' -Value {} -PassThru |
  679.         Add-Member -Name InvokeMember -MemberType 'ScriptMethod' -Value {} -PassThru |
  680.         Add-Member -Name IsAssignableFrom -MemberType 'ScriptMethod' -Value {} -PassThru |
  681.         Add-Member -Name IsDefined -MemberType 'ScriptMethod' -Value {} -PassThru |
  682.         Add-Member -Name IsInstanceOfType -MemberType 'ScriptMethod' -Value {} -PassThru |
  683.         Add-Member -Name IsNormalized -MemberType 'ScriptMethod' -Value {} -PassThru |
  684.         Add-Member -Name IsSubclassOf -MemberType 'ScriptMethod' -Value {} -PassThru |
  685.         Add-Member -Name LastIndexOf -MemberType 'ScriptMethod' -Value {} -PassThru |
  686.         Add-Member -Name LastIndexOfAny -MemberType 'ScriptMethod' -Value {} -PassThru |
  687.         Add-Member -Name MakeArrayType -MemberType 'ScriptMethod' -Value {} -PassThru |
  688.         Add-Member -Name MakeByRefType -MemberType 'ScriptMethod' -Value {} -PassThru |
  689.         Add-Member -Name MakeGenericType -MemberType 'ScriptMethod' -Value {} -PassThru |
  690.         Add-Member -Name MakePointerType -MemberType 'ScriptMethod' -Value {} -PassThru |
  691.         Add-Member -Name MoveTo -MemberType 'ScriptMethod' -Value {} -PassThru |
  692.         Add-Member -Name Normalize -MemberType 'ScriptMethod' -Value {} -PassThru |
  693.         Add-Member -Name PadLeft -MemberType 'ScriptMethod' -Value {} -PassThru |
  694.         Add-Member -Name PadRight -MemberType 'ScriptMethod' -Value {} -PassThru |
  695.         Add-Member -Name Refresh -MemberType 'ScriptMethod' -Value {} -PassThru |
  696.         Add-Member -Name Remove -MemberType 'ScriptMethod' -Value {} -PassThru |
  697.         Add-Member -Name Replace -MemberType 'ScriptMethod' -Value {} -PassThru |
  698.         Add-Member -Name SetAccessControl -MemberType 'ScriptMethod' -Value {} -PassThru |
  699.         Add-Member -Name Split -MemberType 'ScriptMethod' -Value {} -PassThru |
  700.         Add-Member -Name StartsWith -MemberType 'ScriptMethod' -Value {} -PassThru |
  701.         Add-Member -Name Substring -MemberType 'ScriptMethod' -Value {} -PassThru |
  702.         Add-Member -Name ToCharArray -MemberType 'ScriptMethod' -Value {} -PassThru |
  703.         Add-Member -Name ToLower -MemberType 'ScriptMethod' -Value {} -PassThru |
  704.         Add-Member -Name ToLowerInvariant -MemberType 'ScriptMethod' -Value {} -PassThru |
  705.         Add-Member -Name ToUpper -MemberType 'ScriptMethod' -Value {} -PassThru |
  706.         Add-Member -Name ToUpperInvariant -MemberType 'ScriptMethod' -Value {} -PassThru |
  707.         Add-Member -Name Trim -MemberType 'ScriptMethod' -Value {} -PassThru |
  708.         Add-Member -Name TrimEnd -MemberType 'ScriptMethod' -Value {} -PassThru |
  709.         Add-Member -Name TrimStart -MemberType 'ScriptMethod' -Value {} -PassThru |
  710.         Add-Member -Name Chars -MemberType 'NoteProperty' -Value $s.Chars -PassThru
  711.     }
  712.  
  713.  
  714.     if ( "Add-Member" -eq $_cmdlet )
  715.     {
  716.         $global:_dummy = $null
  717.     }
  718.  
  719.  
  720.     if ( "Compare-Object" -eq $_cmdlet )
  721.     {
  722.         $global:_dummy =  (Compare-Object 1 2)[0]
  723.     }
  724.  
  725.  
  726.     if ( "ConvertFrom-SecureString" -eq $_cmdlet )
  727.     {
  728.         $global:_dummy = $null
  729.     }
  730.  
  731.  
  732.     if ( "ConvertTo-SecureString" -eq $_cmdlet )
  733.     {
  734.         $global:_dummy = convertto-securestring "P@ssW0rD!" -asplaintext -force
  735.     }
  736.  
  737.  
  738.     if ( "ForEach-Object" -eq $_cmdlet )
  739.     {
  740.         $global:_dummy = $null
  741.     }
  742.  
  743.  
  744.     if ( "Get-Acl" -eq $_cmdlet )
  745.     {
  746.         $global:_dummy = Get-Acl
  747.     }
  748.  
  749.  
  750.     if ( "Get-Alias" -eq $_cmdlet )
  751.     {
  752.         $global:_dummy = (Get-Alias)[0]
  753.     }
  754.  
  755.  
  756.     if ( "Get-AuthenticodeSignature" -eq $_cmdlet )
  757.     {
  758.         $global:_dummy = Get-AuthenticodeSignature $PSHOME\powershell.exe
  759.     }
  760.  
  761.  
  762.     if ( "Get-ChildItem" -eq $_cmdlet )
  763.     {
  764.         $global:_dummy = $global:_forgci
  765.     }
  766.  
  767.  
  768.     if ( "Get-Command" -eq $_cmdlet )
  769.     {
  770.         $global:_dummy = @(iex $str[$i+1])[0]
  771.     }
  772.  
  773.  
  774.     if ( "Get-Content" -eq $_cmdlet )
  775.     {
  776.         $global:_dummy = (type $PSHOME\profile.ps1)[0]
  777.     }
  778.  
  779.  
  780.     if ( "Get-Credential" -eq $_cmdlet )
  781.     {
  782.         $global:_dummy = $null
  783.     }
  784.  
  785.  
  786.     if ( "Get-Culture" -eq $_cmdlet )
  787.     {
  788.         $global:_dummy = Get-Culture
  789.     }
  790.  
  791.  
  792.     if ( "Get-Date" -eq $_cmdlet )
  793.     {
  794.         $global:_dummy = Get-Date
  795.     }
  796.  
  797.  
  798.     if ( "Get-Event" -eq $_cmdlet )
  799.     {
  800.         $global:_dummy = (Get-Event)[0]
  801.     }
  802.  
  803.  
  804.     if ( "Get-EventLog" -eq $_cmdlet )
  805.     {
  806.         $global:_dummy = Get-EventLog Windows` PowerShell -Newest 1
  807.     }
  808.  
  809.  
  810.     if ( "Get-ExecutionPolicy" -eq $_cmdlet )
  811.     {
  812.         $global:_dummy = Get-ExecutionPolicy
  813.     }
  814.  
  815.  
  816.     if ( "Get-Help" -eq $_cmdlet )
  817.     {
  818.         $global:_dummy = Get-Help Add-Content
  819.     }
  820.  
  821.  
  822.     if ( "Get-History" -eq $_cmdlet )
  823.     {
  824.         $global:_dummy = Get-History -Count 1
  825.     }
  826.  
  827.  
  828.     if ( "Get-Host" -eq $_cmdlet )
  829.     {
  830.         $global:_dummy = Get-Host
  831.     }
  832.  
  833.  
  834.     if ( "Get-Item" -eq $_cmdlet )
  835.     {
  836.         $global:_dummy = $global:_forgci
  837.     }
  838.  
  839.  
  840.     if ( "Get-ItemProperty" -eq $_cmdlet )
  841.     {
  842.         $global:_dummy = $null
  843.     }
  844.  
  845.  
  846.     if ( "Get-Location" -eq $_cmdlet )
  847.     {
  848.         $global:_dummy = Get-Location
  849.     }
  850.  
  851.  
  852.     if ( "Get-Member" -eq $_cmdlet )
  853.     {
  854.         $global:_dummy = (1|Get-Member)[0]
  855.     }
  856.  
  857.  
  858.     if ( "Get-Module" -eq $_cmdlet )
  859.     {
  860.         $global:_dummy = (Get-Module)[0]
  861.     }
  862.  
  863.  
  864.     if ( "Get-PfxCertificate" -eq $_cmdlet )
  865.     {
  866.         $global:_dummy = $null
  867.     }
  868.  
  869.  
  870.     if ( "Get-Process" -eq $_cmdlet )
  871.     {
  872.         $global:_dummy = ps powershell
  873.     }
  874.  
  875.  
  876.     if ( "Get-PSBreakpoint" -eq $_cmdlet )
  877.     {
  878.         $global:_dummy =
  879.         Add-Member -InputObject (New-Object PSObject) -Name Action -MemberType 'NoteProperty' -Value '' -PassThru |
  880.         Add-Member -Name Command -MemberType 'NoteProperty' -Value '' -PassThru |
  881.         Add-Member -Name Enabled -MemberType 'NoteProperty' -Value '' -PassThru |
  882.         Add-Member -Name HitCount -MemberType 'NoteProperty' -Value '' -PassThru |
  883.         Add-Member -Name Id -MemberType 'NoteProperty' -Value '' -PassThru |
  884.         Add-Member -Name Script -MemberType 'NoteProperty' -Value '' -PassThru
  885.     }
  886.  
  887.  
  888.     if ( "Get-PSCallStack" -eq $_cmdlet )
  889.     {
  890.         $global:_dummy = Get-PSCallStack
  891.     }
  892.  
  893.  
  894.     if ( "Get-PSDrive" -eq $_cmdlet )
  895.     {
  896.         $global:_dummy = Get-PSDrive Function
  897.     }
  898.  
  899.  
  900.     if ( "Get-PSProvider" -eq $_cmdlet )
  901.     {
  902.         $global:_dummy = Get-PSProvider FileSystem
  903.     }
  904.  
  905.  
  906.     if ( "Get-PSSnapin" -eq $_cmdlet )
  907.     {
  908.         $global:_dummy = Get-PSSnapin Microsoft.PowerShell.Core
  909.     }
  910.  
  911.  
  912.     if ( "Get-Service" -eq $_cmdlet )
  913.     {
  914.         $global:_dummy = (Get-Service)[0]
  915.     }
  916.  
  917.  
  918.     if ( "Get-TraceSource" -eq $_cmdlet )
  919.     {
  920.         $global:_dummy = Get-TraceSource AddMember
  921.     }
  922.  
  923.  
  924.     if ( "Get-UICulture" -eq $_cmdlet )
  925.     {
  926.         $global:_dummy = Get-UICulture
  927.     }
  928.  
  929.  
  930.     if ( "Get-Variable" -eq $_cmdlet )
  931.     {
  932.         $global:_dummy = Get-Variable _
  933.     }
  934.  
  935.  
  936.     if ( "Get-WmiObject" -eq $_cmdlet )
  937.     {
  938.         $global:_dummy = @(iex $str[$i+1])[0]
  939.     }
  940.  
  941.  
  942.     if ( "Group-Object" -eq $_cmdlet )
  943.     {
  944.         $global:_dummy = 1 | group
  945.     }
  946.  
  947.  
  948.     if ( "Measure-Command" -eq $_cmdlet )
  949.     {
  950.         $global:_dummy = Measure-Command {}
  951.     }
  952.  
  953.  
  954.     if ( "Measure-Object" -eq $_cmdlet )
  955.     {
  956.         $global:_dummy = Measure-Object
  957.     }
  958.  
  959.  
  960.     if ( "New-PSDrive" -eq $_cmdlet )
  961.     {
  962.         $global:_dummy =  Get-PSDrive Alias
  963.     }
  964.  
  965.  
  966.     if ( "New-TimeSpan" -eq $_cmdlet )
  967.     {
  968.         $global:_dummy = New-TimeSpan
  969.     }
  970.  
  971.  
  972.     if ( "Resolve-Path" -eq $_cmdlet )
  973.     {
  974.         $global:_dummy = $PWD
  975.     }
  976.  
  977.  
  978.     if ( "Select-String" -eq $_cmdlet )
  979.     {
  980.         $global:_dummy = " " | Select-String " "
  981.     }
  982.  
  983.  
  984.     if ( "Set-Date" -eq $_cmdlet )
  985.     {
  986.         $global:_dummy =  Get-Date
  987.     }
  988.  
  989.     if ( $property -ne $null)
  990.     {
  991.         foreach ( $name in $property.Split(";", "RemoveEmptyEntries" -as [System.StringSplitOptions]) )
  992.         {
  993.         $global:_dummy = @($global:_dummy.$name)[0]
  994.         }
  995.     }
  996. }
  997.  
  998. function Set-CommandLine ( [string]$script ) { $global:_cmdline.Add($script) }
  999.  
  1000. function Get-CommandLine ( [string]$name ) {
  1001. $name = $name -replace '\?','`?'
  1002. $global:_cmdline -like "$name*"
  1003. }
  1004.  
  1005.  
  1006. function TabExpansion {
  1007.             # This is the default function to use for tab expansion. It handles simple
  1008.             # member expansion on variables, variable name expansion and parameter completion
  1009.             # on commands. It doesn't understand strings so strings containing ; | ( or { may
  1010.             # cause expansion to fail.
  1011.  
  1012.             param($line, $lastWord)
  1013.  
  1014.             & {
  1015.                 # Helper function to write out the matching set of members. It depends
  1016.                 # on dynamic scoping to get $_base, _$expression and $_pat
  1017.                 function Write-Members ($sep='.')
  1018.                 {
  1019.  
  1020.                     # evaluate the expression to get the object to examine...
  1021.                     Invoke-Expression ('$_val=' + $_expression)
  1022.  
  1023.                     if ( $_expression -match '^\$global:_dummy' )
  1024.                     {
  1025.                         $temp = $_expression -replace '^\$global:_dummy(.*)','$1'
  1026.                         $_expression = '$_' + $temp
  1027.                     }
  1028.  
  1029.  
  1030.                     $_method = [Management.Automation.PSMemberTypes] `
  1031.                         'Method,CodeMethod,ScriptMethod,ParameterizedProperty'
  1032.  
  1033.                     if ($sep -eq '.')
  1034.                     {
  1035.                         $members =
  1036.                             (
  1037.                                 [Object[]](Get-Member -InputObject $_val.PSextended $_pat) +
  1038.                                 [Object[]](Get-Member -InputObject $_val.PSadapted $_pat) +
  1039.                                 [Object[]](Get-Member -InputObject $_val.PSbase $_pat)
  1040.                             )
  1041.                         if ( $_val -is [Hashtable] )
  1042.                         {
  1043.                             [Microsoft.PowerShell.Commands.MemberDefinition[]]$_keys = $null
  1044.                             foreach ( $_name in $_val.Keys )
  1045.                             {
  1046.                                 $_keys += `
  1047.                                 New-Object Microsoft.PowerShell.Commands.MemberDefinition `
  1048.                                 [int],$_name,"Property",0
  1049.                             }
  1050.  
  1051.                             $members += [Object[]]$_keys | ? { $_.Name -like $_pat }
  1052.                         }
  1053.  
  1054.                         foreach ($_m in $members | sort membertype,name -Unique)
  1055.                             {
  1056.                                 if ($_m.MemberType -band $_method)
  1057.                                 {
  1058.                                     # Return a method...
  1059.                                     $_base + $_expression + $sep + $_m.name + '('
  1060.                                 }
  1061.                                 else {
  1062.                                     # Return a property...
  1063.                                     $_base + $_expression + $sep + $_m.name
  1064.                                 }
  1065.                             }
  1066.                         }
  1067.  
  1068.                     else
  1069.                     {
  1070.                     foreach ($_m in Get-Member -Static -InputObject $_val $_pat |
  1071.                         Sort-Object membertype,name)
  1072.                        {
  1073.                            if ($_m.MemberType -band $_method)
  1074.                            {
  1075.                                # Return a method...
  1076.                                $_base + $_expression + $sep + $_m.name + '('
  1077.                            }
  1078.                            else {
  1079.                                # Return a property...
  1080.                                $_base + $_expression + $sep + $_m.name
  1081.                            }
  1082.                         }
  1083.                     }
  1084.                 }
  1085.  
  1086.                 switch ([int]$line[-1])
  1087.                 {
  1088.                     # Ctrl+D several date/time formats
  1089.                     4 {
  1090.                         "[DateTime]::Now"
  1091.                         [DateTime]::Now
  1092.                         [DateTime]::Now.ToString("yyyyMMdd")
  1093.                         [DateTime]::Now.ToString("MMddyyyy")
  1094.                         [DateTime]::Now.ToString("yyyyMMddHHmmss")
  1095.                         [DateTime]::Now.ToString("MMddyyyyHHmmss")
  1096.                         'd f g m o r t u y'.Split(" ") | % { [DateTime]::Now.ToString($_) }
  1097.                         break TabExpansion;
  1098.                     }
  1099.  
  1100.                     # Ctrl+K put parentheses behind space or semi-coron or pipe operator
  1101.                     11 {
  1102.                         if ( $line -ne $( "=" + [Char]11 ) )
  1103.                         {
  1104.                             if ( $line[-2] -eq 11 )
  1105.                             {
  1106.                                 $left = '$' + "("
  1107.                                 $line = $line.Substring(0,$line.length-2)
  1108.                             }
  1109.                             else
  1110.                             {
  1111.                                 $left = "("
  1112.                                 $line = $line.Substring(0,$line.length-1)
  1113.                             }
  1114.  
  1115.                             $global:_ctrlk = @()
  1116.                             $l = $line.Length
  1117.                             while ( $l -ge 0 )
  1118.                             {
  1119.                                 $i = $line.Substring(0,$l).LastIndexOfAny(" ;|")
  1120.                                 $global:_ctrlk += $line.Insert($i+1, $left) + ")"
  1121.                                 $l = $i
  1122.                             }
  1123.                             $global:_ctrlk += $line
  1124.                             [Windows.Forms.SendKeys]::SendWait("{Esc}=^K{TAB}")
  1125.                         }
  1126.                         else {
  1127.                             $global:_ctrlk
  1128.                         }
  1129.                         break TabExpansion;
  1130.                     }
  1131.  
  1132.                     # Ctrl+L a part of history commands expansion
  1133.                     12 {
  1134.                         Get-CommandLine $lastWord.SubString(0, $lastword.Length-1)
  1135.                         break TabExpansion;
  1136.                     }
  1137.  
  1138.                     # Ctrl+P hand-operated pop from command buffer stack
  1139.                     16 {
  1140.                         $_base = $lastword.SubString(0, $lastword.Length-1)
  1141.                         $_buf = $global:_cmdstack.Pop()
  1142.                         if ( $_buf.Contains("'") )
  1143.                         {
  1144.                             $line = ($line.SubString(0, $line.Length-1) + $_buf) -replace '([[\]\(\)+{}?~%])','{$1}'
  1145.                             [System.Windows.Forms.SendKeys]::SendWait("{Esc}$line")
  1146.                         }
  1147.                         else {
  1148.                             $_base + $_buf
  1149.                         }
  1150.                         break TabExpansion;
  1151.                     }
  1152.  
  1153.                     # Ctrl+R $Host.UI.RawUI.
  1154.                     18 {
  1155.                         '$Host.UI.RawUI.'
  1156.                         '$Host.UI.RawUI'
  1157.                         break TabExpansion;
  1158.                     }
  1159.  
  1160.                     # Ctrl+V paste clipboard
  1161.                     22 {
  1162.                         $_base = $lastword.SubString(0, $lastword.Length-1)
  1163.                         $global:_clip = New-Object System.Windows.Forms.TextBox
  1164.                         $global:_clip.Multiline = $true
  1165.                         $global:_clip.Paste()
  1166.                         $line = ($line.SubString(0, $line.Length-1) + $global:_clip.Text) -replace '([[\]\(\)+{}?~%])','{$1}'
  1167.                         [System.Windows.Forms.SendKeys]::SendWait("{Esc}$line")
  1168.                         break TabExpansion;
  1169.                     }
  1170.  
  1171.                     # Ctrl+X cut current line
  1172.                     24 {
  1173.                         $_clip = new-object System.Windows.Forms.TextBox;
  1174.                         $_clip.Multiline = $true;
  1175.                         $_clip.Text = $line.SubString(0, $line.Length-1)
  1176.                         $_clip.SelectAll()
  1177.                         $_clip.Copy()
  1178.                         [System.Windows.Forms.SendKeys]::SendWait("{ESC}")
  1179.                         break TabExpansion;
  1180.                     }
  1181.  
  1182.                     # Ctrl+Z cut last word in current line
  1183.                     26 {
  1184.                         $line.SubString(0, $line.Length-1) -match '(^(.*\s)([^\s]*)$)|(^[^\s]*$)' | Out-Null
  1185.                         $_clip = new-object System.Windows.Forms.TextBox;
  1186.                         $_clip.Multiline = $true;
  1187.                         $_clip.Text = $Matches[3]
  1188.                         $_clip.SelectAll()
  1189.                         $_clip.Copy()
  1190.                         [System.Windows.Forms.SendKeys]::SendWait("{ESC}")
  1191.                         $line = $Matches[2] -replace '([[\]\(\)+{}?~%])','{$1}'
  1192.                         [System.Windows.Forms.SendKeys]::SendWait($line)
  1193.                         break TabExpansion;
  1194.                     }
  1195.                 }
  1196.  
  1197.                 switch ( [int]$line[-2] )
  1198.                 {
  1199.  
  1200.                     # Ctrl+K put character behind space or semi-coron or pipe operator
  1201.                     11 {
  1202.                         switch ( $line[-1] )
  1203.                         {
  1204.                             '{' { $right = '}' }
  1205.                             '(' { $right = ')' }
  1206.                             '[' { $right = ']' }
  1207.                         }
  1208.  
  1209.                         $left = $line[-1]
  1210.                         $delimiter = " ;|"
  1211.  
  1212.                         if ( $line[-3] -eq 11 )
  1213.                         {
  1214.                             if ( $line[-1] -eq '%' -or $line[-1] -eq '?' )
  1215.                             {
  1216.                                 $left = ' ' + $left + ' {'; $right = '}'
  1217.                                 $delimiter = '|'
  1218.                             }
  1219.                             else
  1220.                             {
  1221.                                 $left = '$' + $left
  1222.                             }
  1223.                             $line = $line.Substring(0,$line.length-3)
  1224.                         }
  1225.                         else
  1226.                         {
  1227.                             $line = $line.Substring(0,$line.length-2)
  1228.                         }
  1229.  
  1230.                         $global:_ctrlk = @()
  1231.                         $l = $line.Length
  1232.                         while ( $l -ge 0 )
  1233.                         {
  1234.                             $i = $line.Substring(0,$l).LastIndexOfAny($delimiter)
  1235.                             $global:_ctrlk += $line.Insert($i+1, $left) + $right
  1236.                             $l = $i
  1237.                         }
  1238.                         $global:_ctrlk += $line
  1239.                         [Windows.Forms.SendKeys]::SendWait("{Esc}=^K{TAB}")
  1240.                         break TabExpansion;
  1241.                     }
  1242.  
  1243.                     # Ctrl+X cut words with a character after Ctrl+X until the character
  1244.                     24 {
  1245.                         $line.SubString(0, $line.Length-2) -match "(^(.*$($line[-1]))([^$($line[-1])]*)`$)|(^[^\$($line[-1])]*`$)" | Out-Null
  1246.                         $_clip = new-object System.Windows.Forms.TextBox;
  1247.                         $_clip.Multiline = $true;
  1248.                         $_clip.Text = $Matches[3]
  1249.                         $_clip.SelectAll()
  1250.                         $_clip.Copy()
  1251.                         [System.Windows.Forms.SendKeys]::SendWait("{ESC}")
  1252.                         $line = $Matches[2] -replace '([[\]\(\)+{}?~%])','{$1}'
  1253.                         [System.Windows.Forms.SendKeys]::SendWait($line)
  1254.                         break TabExpansion;
  1255.                     }
  1256.                 }
  1257.  
  1258.                 switch -regex ($lastWord)
  1259.                 {
  1260.  
  1261.                     # Handle property and method expansion at '$_'
  1262.                     '(^.*)(\$_\.)(\w*)$' {
  1263.                         $_base = $matches[1]
  1264.                         $_expression = '$global:_dummy'
  1265.                         $_pat = $matches[3] + '*'
  1266.                         $global:_dummy = $null
  1267.                         Get-PipeLineObject
  1268.                         if ( $global:_dummy -eq $null )
  1269.                         {
  1270.  
  1271.                             if ( $global:_exp -match '^\$.*\(.*$' )
  1272.                             {
  1273.                                 $type = ( iex $_exp.Split("(")[0] ).OverloadDefinitions[0].Split(" ")[0] -replace '\[[^\[\]]*\]$' -as [type]
  1274.  
  1275.                                 if ( $_expression -match '^\$global:_dummy' )
  1276.                                 {
  1277.                                     $temp = $_expression -replace '^\$global:_dummy(.*)','$1'
  1278.                                     $_expression = '$_' + $temp
  1279.                                 }
  1280.  
  1281.                                 foreach ( $_m in $type.GetMembers() | sort membertype,name | group name | ? { $_.Name -like $_pat } | % { $_.Group[0] } )
  1282.                                 {
  1283.                                    if ($_m.MemberType -eq "Method")
  1284.                                    {
  1285.                                        $_base + $_expression + '.' + $_m.name + '('
  1286.                                    }
  1287.                                    else {
  1288.                                        $_base + $_expression + '.' + $_m.name
  1289.                                    }
  1290.                                 }
  1291.                                 break;
  1292.                             }
  1293.                             elseif ( $global:_exp -match '^\[.*\:\:.*\(.*$' )
  1294.                             {
  1295.                                 $tname, $mname = $_exp.Split(":(", "RemoveEmptyEntries"-as [System.StringSplitOptions])[0,1]
  1296.                                 $type = @(iex ($tname + '.GetMember("' + $mname + '")'))[0].ReturnType.FullName -replace '\[[^\[\]]*\]$' -as [type]
  1297.  
  1298.                                 if ( $_expression -match '^\$global:_dummy' )
  1299.                                 {
  1300.                                     $temp = $_expression -replace '^\$global:_dummy(.*)','$1'
  1301.                                     $_expression = '$_' + $temp
  1302.                                 }
  1303.  
  1304.                                 foreach ( $_m in $type.GetMembers() | sort membertype,name | group name | ? { $_.Name -like $_pat } | % { $_.Group[0] } )
  1305.                                 {
  1306.                                    if ($_m.MemberType -eq "Method")
  1307.                                    {
  1308.                                        $_base + $_expression + '.' + $_m.name + '('
  1309.                                    }
  1310.                                    else {
  1311.                                        $_base + $_expression + '.' + $_m.name
  1312.                                    }
  1313.                                 }
  1314.                                 break;
  1315.                             }
  1316.                             elseif ( $global:_exp -match '^(\$\w+(\[[0-9,\.]+\])*(\.\w+(\[[0-9,\.]+\])*)*)$' )
  1317.                             {
  1318.                                 $global:_dummy = @(iex $Matches[1])[0]
  1319.                             }
  1320.                             else
  1321.                             {
  1322.                                 $global:_dummy =  $global:_mix
  1323.                             }
  1324.                         }
  1325.  
  1326.                         Write-Members
  1327.                         break;
  1328.                     }
  1329.  
  1330.                     # Handle property and method expansion rooted at variables...
  1331.                     # e.g. $a.b.<tab>
  1332.                     '(^.*)(\$(\w|\.)+)\.(\w*)$' {
  1333.                         $_base = $matches[1]
  1334.                         $_expression = $matches[2]
  1335.                         [void] ( iex "$_expression.IsDataLanguageOnly" ) # for [ScriptBlock]
  1336.                         $_pat = $matches[4] + '*'
  1337.                         if ( $_expression -match '^\$_\.' )
  1338.                         {
  1339.                             $_expression = $_expression -replace '^\$_(.*)',('$global:_dummy' + '$1')
  1340.                         }
  1341.                         Write-Members
  1342.                         break;
  1343.                     }
  1344.  
  1345.                     # Handle simple property and method expansion on static members...
  1346.                     # e.g. [datetime]::n<tab>
  1347.                     '(^.*)(\[(\w|\.)+\])\:\:(\w*)$' {
  1348.                         $_base = $matches[1]
  1349.                         $_expression = $matches[2]
  1350.                         $_pat = $matches[4] + '*'
  1351.                         Write-Members '::'
  1352.                         break;
  1353.                     }
  1354.  
  1355.                     # Handle complex property and method expansion on static members
  1356.                     # where there are intermediate properties...
  1357.                     # e.g. [datetime]::now.d<tab>
  1358.                     '(^.*)(\[(\w|\.)+\]\:\:(\w+\.)+)(\w*)$' {
  1359.                         $_base = $matches[1]  # everything before the expression
  1360.                         $_expression = $matches[2].TrimEnd('.') # expression less trailing '.'
  1361.                         $_pat = $matches[5] + '*'  # the member to look for...
  1362.                         Write-Members
  1363.                         break;
  1364.                     }
  1365.  
  1366.                     # Handle property and method expansion rooted at variables...
  1367.                     # e.g. { 1+1 }.inv<tab>
  1368.                     '(^.*})\.(\w*)$' {
  1369.                         $_base = $matches[1]
  1370.                         $_pat = $matches[2] + '*'
  1371.                         foreach ( $_m in {} | Get-Member $_pat | sort membertype,name )
  1372.                         {
  1373.                             if ($_m.MemberType -eq "Method")
  1374.                             {
  1375.                                 $_base + '.' + $_m.name + '('
  1376.                             }
  1377.                             else {
  1378.                                 $_base + '.' + $_m.name
  1379.                             }
  1380.                         }
  1381.                         break;
  1382.                     }
  1383.  
  1384.                     # Handle variable name expansion...
  1385.                     '(^.*\$)(\w+)$' {
  1386.                         $_prefix = $matches[1]
  1387.                         $_varName = $matches[2]
  1388.                         foreach ($_v in Get-ChildItem ('variable:' + $_varName + '*'))
  1389.                         {
  1390.                             $_prefix + $_v.name
  1391.                         }
  1392.                         break;
  1393.                     }
  1394.  
  1395.                     # Handle item name expansion in variable notation...
  1396.                     '(^.*\${)(((\w+):([\\/]?))[^\\/]+([\\/][^\\/]*)*)$' {
  1397.                         $_prefix = $matches[1]
  1398.                         $_driveName = $matches[3]
  1399.                         $_itemName = $matches[2] + "*"
  1400.                         Get-ChildItem $_itemName  |
  1401.                         % {
  1402.                             if ( $matches[5] -and $_.PSProvider.Name -eq "FileSystem" )
  1403.                             {
  1404.                                 $output = $_prefix + $_.FullName
  1405.                             }
  1406.                             elseif ( $_.PSProvider.Name -eq "FileSystem" -and $matches[6] )
  1407.                             {
  1408.                                 $cd = (Get-Location -PSDrive $matches[4]).Path
  1409.                                 $output = $_prefix + $_driveName + $_.FullName.Substring($cd.Length+1)
  1410.                             }
  1411.                             elseif ( $_.PSProvider.Name -eq "FileSystem" )
  1412.                             {
  1413.                                 $cd = (Get-Location -PSDrive $matches[4]).Path
  1414.                                 $output = $_prefix + $_driveName + $_.FullName.Substring($cd.Length+1)
  1415.                             }
  1416.                             else
  1417.                             {
  1418.                                 $output = $_prefix + $_driveName + $_.Name
  1419.                             }
  1420.  
  1421.                             if ( $_.PSIsContainer )
  1422.                             {
  1423.                                 $output
  1424.                             }
  1425.                             else
  1426.                             {
  1427.                                 $output + "}"
  1428.                             }
  1429.                         }
  1430.                         break;
  1431.                     }
  1432.  
  1433.                     # Handle PSDrive name expansion in variable notation...
  1434.                     '(^.*\${)(\w+)$' {
  1435.                         $_prefix = $matches[1]
  1436.                         $_driveName = $matches[2] + "*"
  1437.                         Get-PSDrive $_driveName | % { $_prefix + $_ + ':' }
  1438.                         break;
  1439.                     }
  1440.  
  1441.                     # Handle env&function drives variable name expansion...
  1442.                     '(^.*\$)(.*\:)(\w+)$' {
  1443.                         $_prefix = $matches[1]
  1444.                         $_drive = $matches[2]
  1445.                         $_varName = $matches[3]
  1446.                         if ($_drive -eq "env:" -or $_drive -eq "function:")
  1447.                         {
  1448.                             foreach ($_v in Get-ChildItem ($_drive + $_varName + '*'))
  1449.                             {
  1450.                                 $_prefix + $_drive + $_v.name
  1451.                             }
  1452.                         }
  1453.                         break;
  1454.                     }
  1455.  
  1456.                     # Handle array's element property and method expansion
  1457.                     # where there are intermediate properties...
  1458.                     # e.g. foo[0].n.b<tab>
  1459.                     '(^.*)(\$((\w+\.)|(\w+(\[(\w|,)+\])+\.))+)(\w*)$'
  1460.                     {
  1461.                         $_base = $matches[1]
  1462.                         $_expression = $matches[2].TrimEnd('.')
  1463.                         $_pat = $Matches[8] + '*'
  1464.                         [void] ( iex "$_expression.IsDataLanguageOnly" ) # for [ScriptBlock]
  1465.                         if ( $_expression -match '^\$_\.' )
  1466.                         {
  1467.                             $_expression = $_expression -replace '^\$_(.*)',('$global:_dummy' + '$1')
  1468.                         }
  1469.                         Write-Members
  1470.                         break;
  1471.                     }
  1472.  
  1473.                     # Handle property and method expansion rooted at type object...
  1474.                     # e.g. [System.Type].a<tab>
  1475.                     '(^\[(\w|\.)+\])\.(\w*)$'
  1476.                     {
  1477.                         if ( $(iex $Matches[1]) -isnot [System.Type] ) { break; }
  1478.                         $_expression = $Matches[1]
  1479.                         $_pat = $Matches[$matches.Count-1] + '*'
  1480.                         Write-Members
  1481.                         break;
  1482.                     }
  1483.  
  1484.                     # Handle complex property and method expansion on type object members
  1485.                     # where there are intermediate properties...
  1486.                     # e.g. [datetime].Assembly.a<tab>
  1487.                     '^(\[(\w|\.)+\]\.(\w+\.)+)(\w*)$' {
  1488.                         $_expression = $matches[1].TrimEnd('.') # expression less trailing '.'
  1489.                         $_pat = $matches[4] + '*'  # the member to look for...
  1490.                         if ( $(iex $_expression) -eq $null ) { break; }
  1491.                         Write-Members
  1492.                         break;
  1493.                     }
  1494.  
  1495.                     # Handle property and method expansion rooted at close parenthes...
  1496.                     # e.g. (123).a<tab>
  1497.                     '^(.*)\)((\w|\.)*)\.(\w*)$' {
  1498.                         $_base = $Matches[1] + ")"
  1499.                         if ( $matches[3] -eq $null) { $_expression = '[System.Type]' }
  1500.                         else { $_expression = '[System.Type]' + $Matches[2] }
  1501.                         $_pat = $matches[4] + '*'
  1502.                         iex "$_expression | Get-Member $_pat | sort MemberType,Name" |
  1503.                         % {
  1504.                             if ( $_.MemberType -like "*Method*" -or $_.MemberType -like "*Parameterized*" ) { $parenthes = "(" }
  1505.                             if ( $Matches[2] -eq "" ) { $_base + "." + $_.Name + $parenthes }
  1506.                             else { $_base + $Matches[2] + "." + $_.Name + $parenthes }
  1507.                           }
  1508.                         break;

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me