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

[玩转系统] PowerShell 变量通配符

作者:精品下载站 日期:2024-12-14 22:05:12 浏览:13 分类:玩电脑

PowerShell 变量通配符


PowerShell 变量通配符允许您通过模式匹配编写功能强大的脚本。

PowerShell 中使用两种主要类型的通配符 *? 进行模式匹配。

  • * - 匹配零个或多个字符
  • ? - 精确匹配一个字符

在本教程中,我们将讨论如何将通配符与 PowerShell 变量和实际示例一起使用来增强脚本。

对 PowerShell 变量使用通配符

使用-Like运算符

要将变量与通配符模式进行比较,请使用 Like 运算符来比较使用通配符模式的字符串。

$temp = "ShellGeek"                                                                                             $result = $temp -like "Shell*" 

在上面的 PowerShell 脚本中,$temp 变量存储字符串类型值。 -like 运算符使用通配符模式 * 比较字符串,以检查字符串是否与给定字符串中的一个或多个字符匹配。

上述 PowerShell 脚本的输出是:

[玩转系统] PowerShell 变量通配符

带有通配符的Where-Object cmdlet

Where-Object cmdlet 通常与通配符一起使用,以根据对象的属性过滤对象。

$files = Get-ChildItem -Path 'D:\PS\'                                                                           $textFiles = $files | Where-Object {$_.name -like "*.txt"}                                                      $textFiles                                                                                                      

在上面的 PowerShell 脚本中,Get-ChildItem cmdlet 从指定路径检索项目并将它们存储在变量 $files 中。

Where-Object cmdlet 用于 $files 变量来过滤文件扩展名如 *.txt 的文件,并将它们存储在$textFiles 变量。

使用 PowerShell cmdlet 中的通配符检索文本文件的上述 PowerShell 脚本的输出为:

PS D:\> $files = Get-ChildItem -Path 'D:\PS\'                                                                           PS D:\> $textFiles = $files | Where-Object {$_.name -like "*.txt"}                                                      PS D:\> $textFiles                                                                                                      

    Directory: D:\PS


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       26-02-2023     16:10             87 comp.txt
-a----       02-04-2023     19:32              0 emptyFile.txt


PS D:\>      

使用 ? PowerShell 中带有变量的通配符

? PowerShell 中的通配符可查找与一个字符完全匹配的模式。

Get-ChildItem -Path 'D:\PS\' ??.* -File 

在上面的 PowerShell 脚本中,Get-ChildItem cmdlet 使用 Path 参数指定目录名称来查找其中只有 2 个字符的文件名。

上述 PowerShell 脚本的输出是:

PS D:\> Get-ChildItem -Path 'D:\PS\' ??.* -File                                                                         

    Directory: D:\PS


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       07-04-2023     12:10              0 ad.txt

根据名称模式过滤文件

在 PowerShell 中,通配符 * 可以与 -like 运算符一起使用,以根据名称模式过滤目录中的文件。

$FilePattern = "powershell*"                                                                                    $imageFiles = Get-ChildItem -Path 'D:\PS\' | Where-Object {$_.Name -like $FilePattern}   

在上面的 PowerShell 脚本中,Get-ChildItem 使用 Path 变量获取所有项目,并将它们通过管道传输到 Where-Object cmdlet根据名称模式检索文件。

上述 PowerShell 脚本的输出是:

PS D:\> $FilePattern = "powershell*"                                                                                    PS D:\> $imageFiles = Get-ChildItem -Path 'D:\PS\' | Where-Object {$_.Name -like $FilePattern}                          PS D:\> $imageFiles                                                                                                     

    Directory: D:\PS


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       02-04-2023     20:51          15400 powershell-clear-variable.png
-a----       07-04-2023     09:47          17358 powershell-convert-variables-to-string.png
-a----       02-04-2023     19:37           7396 powershell-create-empty-file.png
-a----       02-04-2023     17:37          10133 powershell-get-temp-folder.png
-a----       07-04-2023     08:31          12163 powershell-is-null-or-empty-check-for-variable.png
-a----       02-04-2023     22:04          19830 powershell-read-only-variable.png
-a----       02-04-2023     18:10          10850 powershell-temporary-file.png
-a----       07-04-2023     11:58           7162 powershell-variable-wildcards.png


PS D:\> 

酷提示:如何在 PowerShell 中的 Path 中使用变量!

按名称模式查找进程

通配符 * 可在 PowerShell 中使用,根据名称模式查找系统上运行的进程。

$processPattern = "chrome*"                                                                                     $services = Get-Process | Where-Object {$_.ProcessName -like $processPattern}                                   $services 

结论

希望上面关于如何使用 PowerShell 变量通配符与 *? 的文章对您有所帮助。

通配符使您能够创建和编写灵活的搜索模式并提高脚本效率。

您可以在 ShellGeek 主页上找到有关 PowerShell Active Directory 命令和 PowerShell 基础知识的更多主题。

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

取消回复欢迎 发表评论:

关灯