当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell 通配符 | PowerShell 中通配符表达式的类型

作者:精品下载站 日期:2024-12-14 04:52:01 浏览:16 分类:玩电脑

PowerShell 通配符 | PowerShell 中通配符表达式的类型


[玩转系统] PowerShell 通配符 | PowerShell 中通配符表达式的类型

PowerShell 通配符简介

通配符是用于匹配多个字符的模式。它们用于在 cmdlet 中指定某些模式以过滤结果或仅返回与指定的通配符匹配的结果。 PowerShell 中提供四种类型的通配符。它们表示为*、?、[m-n]和[abc]。可以一起使用多个通配符模式。 PowerShell 中的许多 cmdlet 都接受通配符参数。接受通配符作为输入的 Cmdlet 不区分大小写。还可以在 cmdlet 中使用通配符来筛选 cmdlet 结果中可用的属性。本文将详细介绍通配符、通配符的类型、用法以及各种示例。

PowerShell 中的通配符类型

PowerShell 中提供四种类型的通配符。

语法:

1) *: 表示匹配模式必须是零个或多个字符。如果仅使用 *,则返回所有匹配的字符。

示例:Ba* 它将返回 bagubali、batcha、basel。它不会像美国、非洲那样回归。

2) ?: 表示该位置的字符必须匹配。

示例:?b 将返回 ab、cb、db,不会返回 aa、ac、aj。

3) []:这表示必须检查范围内的模式是否匹配。

示例:[a-c]de 将返回 ade、bde,并且不会与 zde 匹配。

4) []: 仅匹配范围内提到的特定字符。

示例:[ab]tef 将与 atef 和 btef 匹配。

实施 PowerShell 通配符的示例

以下是 PowerShell 通配符的示例:

例子#1

代码:

Write-Host "Welcome to wild card in powershell example"
Write-Host "Welcome to wild card in powershell example"
Write-Host "welcome to * wild card demo"
Write-Host "listing all txt files inside a folder"
Get-ChildItem C:\Users\EDUCBA\*.txt
write-host "Filtering services running on the machine"
Get-Service | Where-Object {$_.Name -like "net*"}
Write-Host "listing all excel files inside a folder"
Get-ChildItem C:\Users\EDUCBA\*.xlsx
Write-Host "Recurrsing through a sub folders and finding all text files"
Get-ChildItem -Path C:\Users\EDUCBA\*.txt -Recurse -Force
Write-Host "Recurrsing through a sub folders and finding all excel files"
Get-ChildItem -Path C:\Users\EDUCBA\*.xlsx -Recurse -Force

输出:

[玩转系统] PowerShell 通配符 | PowerShell 中通配符表达式的类型

例子#2

代码:

Write-Host "Welcome to wild card in powershell example"
Write-Host "welcome to ? wild card demo"
Write-Host "listing services"
Get-Service | Where-Object {$_.Name -like "???logon"}
write-host "Filtering services running on the machine"
Get-Service | Where-Object {$_.Name -like "??logon"}
write-host "listing services running on the machine"
Get-Service | Where-Object {$_.Name -like "?logon"}
Write-Host "Recurrsing through a sub folders and finding all text files"
Get-ChildItem -Path C:\Users\EDUCBA\*.txt -Recurse -Force
Write-Host "Recurrsing through a sub folders and finding all excel files"
Get-ChildItem -Path C:\Users\EDUCBA\*.xlsx -Recurse -Force

输出:

[玩转系统] PowerShell 通配符 | PowerShell 中通配符表达式的类型

例子#3

代码:

Write-Host "Demo of wild card charcaters in powershell"
Write-Host "demo of [] wild card pattern"
$input=@("one","two","three","four","five","oneone","onetwo","onethree","aone","bone","cone")
Write-Host "Word contains one"
$input -like "[a-z]one"
$input=@("abone","two","three","abfour","five","aboneone","onetwo","onethree","aone","bone","abonethree")
Write-Host "word contains abone"
$input -like "[ab]one"
$input=@("abone","two","three","abfour","five","aboneone","onetwo","onethree","aone","bone","abonethree")
Write-Host "word contains multiple filters"
$input -like "[a-d][b-f]one"

输出:

[玩转系统] PowerShell 通配符 | PowerShell 中通配符表达式的类型

例子#4

代码:

Write-Host "Demo of wild card charcaters in powershell"
Write-Host "demo of match specific characters wild card pattern"
$input=@("one","two","three","four","five","oneone","onetwo","onethree","aone","bone","cone")
Write-Host "Word contains one"
$input -like "[o]ne"
$input=@("abone","two","three","afour","five","aboneone","onetwo","onethree","aone","bone","abonethree","cfour","bfour","dfour")
Write-Host "word contains four"
$input -like "[abcd]four"
$input=@("abone","two","three","abfour","five","aboneone","onetwo","onethree","aone","bone","abonethree")
Write-Host "word contains multiple filters"
$input -like "[a-d][b-f]one"

输出:

[玩转系统] PowerShell 通配符 | PowerShell 中通配符表达式的类型

例子#5

代码:

Write-Host "Demo of wild card charcaters in powershell"
Write-Host "demo of multiple wild card pattern"
Write-Host "Applying multiple wild card pattern in list of services"
Get-Service | Where-Object {$_.Name -like "*n?t*"}
$inout=@("vignesh","nandhini","vyapini","ashiwini","hini","madhu")
$inout -like "[a-z][a-d]*"
$inout=@("vignesh","nandhini","vyapini","ashiwini","hini","madhu")
$inout -like "*ne[s-z]?o*"
$inout=@("vignesh","nandhini","vyapini","ashiwini","hini","madhu")
$inout -like "*ne[s-z]?"

输出:

[玩转系统] PowerShell 通配符 | PowerShell 中通配符表达式的类型

例子#6

代码:

write-host "Demo of wild card operator"
Write-Host "Match with wildcard charcaters for a single string"
$input="My name is ramesh"
$input -match "na*"
Write-Host "Match without wildcard charcaters for a single string"
$input -match "na"
Write-Host "Match with wildcard charcaters for a collection"
$input="abc", "abcd", "abdsf","erert"
$input -match "ab*"
Write-Host "Match without wildcard charcaters for a collection"
$input="abc", "abcd", "abdsf","erert"
$input -match "ab"
Write-Host "Not Match with wildcard charcaters for a single string"
$input="My name is sacinn tendulkar"
$input -notmatch "za*?"
Write-Host "Not Match without wildcard charcaters for a single string"
$input -notmatch "za"
Write-Host "Not Match with wildcard charcaters for a collection"
$input="abc", "abcd", "abdsf","erert"
$input -notmatch "za[-z]*"
Write-Host "Not Match without wildcard charcaters for a collection"
$input="abc", "abcd", "abdsf","erert"
$input -notmatch "za[u]"

输出:

[玩转系统] PowerShell 通配符 | PowerShell 中通配符表达式的类型

例子#7

代码:

Write-Host "Example of Wild card usage in PowerShell"
Write-Host "demo of match specific characters wild card pattern"
$input=@("test","test1","test2","test3","test4","test1test","test1test","test2test","test3test","test4test","test5test")
Write-Host "Word contains test"
$input -like "[t]est*"
$input=@("test","test1","test2","test3","test4","test1test","test1test","test2test","test3test","test4test","test5test")
Write-Host "word contains test1"
$input -like "test*"
$input=@("test","1test1","test2","test3","test4","test1test","test1test","test2test","test3test","test4test","test5test")
Write-Host "word contains multiple filters"
$input -like "?test1"
Write-Host "Demo of wild card charcaters in powershell"
Write-Host "demo of multiple wild card pattern"
Write-Host "Applying multiple wild card pattern in list of services"
Get-Service | Where-Object {$_.Name -like "*n?t*"}
$inout=@("test","test1","test2","test3","test4","test1test","test1test","test2test","test3test","test4test","test5test")
$inout -like "[a-z][a-d]*"
$inout=@("test","test1","test2","test3","test4","test1test","test1test","test2test","test3test","test4test","test5test")
$inout -like "*ne[s-z]?o*"
$inout=@("test","test1","test2","test3","test4","test1test","test1test","test2test","test3test","test4test","test5test")
$inout -like "*ne[s-z]?"

输出:

[玩转系统] PowerShell 通配符 | PowerShell 中通配符表达式的类型

例子#8

代码:

Write-Host "Example of wild card characters"
$input= "sdfsdfdsf","adsadsad","test","test1"
$input -contains "test*"
$input= "sdfsdfdsf","adsadsad","test","test1"
$input -contains "dfsdf?*"
Write-Host "Demo of multiple check values wild card"
$input="one","two","three","four","five"
$input -contains "one?","two*"
$input="one","two"
$input, "three","four" -ccontains $input
$input= "sdfsdfdsf","adsadsad","dfdf","sdf"
$input -notcontains "test"
$input= "sdfsdfdsf","adsadsad","test","test1"
$input -notcontains "test"
$input="one","two","three","four","five"
$input -notcontains "one","two"
$input="one","two"
$input, "three","four" -notcontains $input

输出:

[玩转系统] PowerShell 通配符 | PowerShell 中通配符表达式的类型

例子#9

代码:

Write-Host "Demo of wild card charcaters in powershell"
Write-Host "demo of [] wild card pattern"
$input=@("india","china","america","africa","thailand","sweden","germany","poland","austria","belgium","srilanka")
Write-Host "country staring with a"
$input -like "a*"
$input=@("india","china","america","africa","thailand","sweden","germany","poland","austria","belgium","srilanka")
Write-Host "word contains be"
$input -like "be*"
$input=@("india","china","america","africa","thailand","sweden","germany","poland","austria","belgium","srilanka")
Write-Host "word contains multiple filters"
$input -like "[a-d][b-f]*"

输出:

[玩转系统] PowerShell 通配符 | PowerShell 中通配符表达式的类型

结论

因此,本文详细介绍了 PowerShell 中的通配符表达式。它还通过适当的示例解释了可用的通配符表达式的类型及其用法。文章详细解释了每种通配符表达式类型及其相应的示例程序。还详细解释了如何一起使用多个通配符表达式以及适当的演示程序。要了解更多详细信息,建议编写并练习示例程序。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯