重點整理自Let's do a deep drill into Select-String by Jeffrey Snover , Windows PowerShell/MMC Architect

  • $c="a"
    get-alias ($c+"*")   #參數有變數 則用()包住  ; 等於get-alias a*
  • Select-String accepts arrays of wildcards to specify Files
    • select-string  欲搜尋字串  欲搜尋之檔案路徑(可為wildcard)
      • select-string  drive *  #搜尋目前路徑下所有檔案中包含*drive*的字串(預設視為前後加*)
      • 被搜尋到的對象不能為資料夾???
      • select-string  drive  [a-m]* #搜尋名為a-m開頭的檔案
      • select-string  drive  drive g*,r* #搜尋名為g,r開頭的檔案
  • Select-String accepts arrays of regular expressions to specify STRINGS
    • 欲搜尋字串可為regular expression
      • " [a-r]dr" *
      • " [a-r]dr|mount"
      • " [a-n]dr"," [m-z]dr"
    • 若不想讓搜尋字串被視為regular expression 則要指定 -SimpleMatch
  • Select-String accepts –Include and –Exclude to tweak which files it operates on
    • 欲搜尋檔案可利用–Include 包含某檔案
    • 欲搜尋檔案可利用–Exclude 排除某檔案
    • These parameters take a wildcard expression or set of wildcard expressions and operate AFTER the filepath is resolved
      • -exclude *[mn]-al*   #完整檔案路徑中(不只是檔名)不包含*[mn]-al*
      • -exclude [mn]-al*    #完整檔案路徑中不包含[mn]-al*
  • You can pipe anything that produces FileInfo objects into Select-String
    • 欲搜尋之檔案可由pipeline餵近來
    • dir [g-r]* |select-string  drive 
    • ([System.IO.DirectoryInfo]"c:\sstest").GetFileSystemInfos() |  select-string  drive
  • You can pipe anything that produces MatchInfo objects into Select-String
    • select-string get *|select-string item
  • Select-String outputs MatchInfo objects not Strings
    • Microsoft.PowerShell.Commands.MatchInfo
    • select-string  "stop","new.*ve" *|Format-List *
    • select-string  "stop","new.*ve" *|group Pattern
  • Sometimes you just want the first match in a file
    • select-string item r* -list

    全站熱搜

    ayowu 發表在 痞客邦 留言(1) 人氣()