获取帮助

  1. sunzhengbo in Desktop get-help ls
  2. NAME
  3. Get-ChildItem
  4. SYNOPSIS
  5. Gets the items and child items in one or more specified locations.
  6. SYNTAX
  7. Get-ChildItem [[-Filter] <System.String>] [-Attributes {Archive | Compressed | Device | Directory | Encrypted | Hidden | IntegrityStream | Normal | NoScrubData | NotContentIndexed | Offline | ReadOnly | ReparsePoint | SparseFile |
  8. System | Temporary}] [-Depth <System.UInt32>] [-Directory] [-Exclude <System.String[]>] [-File] [-FollowSymlink] [-Force] [-Hidden] [-Include <System.String[]>] -LiteralPath <System.String[]> [-Name] [-ReadOnly] [-Recurse] [-System
  9. ] [<CommonParameters>]

过滤

过滤对象(where-object)

获取 % 的别名信息
  1. sunzhengbo in Desktop get-alias | where-object Name -eq %
  2. CommandType Name Version Source
  3. ----------- ---- ------- ------
  4. Alias % -> ForEach-Object

获取 % 和 ? 的别名信息
  1. sunzhengbo in Desktop get-alias | where-object Name -in %, ?
  2. CommandType Name Version Source
  3. ----------- ---- ------- ------
  4. Alias ? -> Where-Object
  5. Alias % -> ForEach-Object

过滤文本(select-string)

测试的文本
  1. sunzhengbo in Desktop cat .\aa.txt
  2. Import-Module posh-git
  3. Import-Module oh-my-posh
  4. Set-PoshPrompt -Theme spaceship

将行中包含某个字符的行过滤出来
  1. sunzhengbo in Desktop cat .\aa.txt | select-string import
  2. Import-Module posh-git
  3. Import-Module oh-my-posh

显示文本的第一列
  1. sunzhengbo in Desktop cat .\aa.txt | select-string import | %{($_ -split ' ')[0]}
  2. Import-Module
  3. Import-Module

显示文本的第后列
  1. sunzhengbo in Desktop cat .\aa.txt | select-string import | %{($_ -split ' ')[-1]}
  2. posh-git
  3. oh-my-posh

显示文本的1-2列
  1. sunzhengbo in Desktop cat .\aa.txt | %{"$(($_ -split ' ')[0..1])"}
  2. Import-Module posh-git
  3. Import-Module oh-my-posh
  4. Set-PoshPrompt -Theme

显示文本的第1列,第2列
  1. sunzhengbo in Desktop cat .\aa.txt | %{"$(($_ -split ' ')[0,1])"}
  2. Import-Module posh-git
  3. Import-Module oh-my-posh
  4. Set-PoshPrompt -Theme

文本根据多个条件拆分

关于 .. 运算符,有一点需要注意。 序列 0..-1 和 -1..0 的计算结果为值 0,-1 和 -1,0。 如果忘记了这一细节,就很容易看到 $data[0..-1],并认为它会枚举所有项

  1. sunzhengbo in Desktop cat .\aa.txt | %{"$(($_ -split {$_ -eq ' ' -or $_ -eq '-'})[0..2])"}
  2. Import Module posh
  3. Import Module oh
  4. Set PoshPrompt