[玩转系统] PowerShell 通配符 | PowerShell 中通配符表达式的类型
作者:精品下载站 日期:2024-12-14 04:52:01 浏览:16 分类:玩电脑
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
输出:
例子#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
输出:
例子#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"
输出:
例子#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"
输出:
例子#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]?"
输出:
例子#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]"
输出:
例子#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]?"
输出:
例子#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
输出:
例子#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 中的通配符表达式。它还通过适当的示例解释了可用的通配符表达式的类型及其用法。文章详细解释了每种通配符表达式类型及其相应的示例程序。还详细解释了如何一起使用多个通配符表达式以及适当的演示程序。要了解更多详细信息,建议编写并练习示例程序。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag