首页 > 解决方案 > Powershell 中用户输入的通配符功能

问题描述

我目前正在编写一个 powershell 脚本,该脚本接受用户输入并搜索目录中具有相似名称的所有项目。

$filename = '*test*.*'
$searchinfolder = 'C:\example'
Get-ChildItem -Path $searchinfolder -Filter $filename -Recurse | %{$_.FullName}

我需要的是让它接受用户输入,同时保持第一行的功能。到目前为止,我使用过:

 $filename = Read-Host -Prompt 'Input word'

我必须做些什么来维护功能?例如,它找到文件夹 test 和 test1

标签: powershellinputwildcard

解决方案


@Lee_Dailey 在评论中给出了解决方案。

$filename = Read-Host -Prompt 'Input word'
$filename = '*{0}*.*' -f $fileName
Get-ChildItem -Path $searchinfolder -Filter $filename -Recurse | %{$_.FullName}

推荐阅读