いろいろ備忘録

雑記です。

PowerShellでファイルを検索してショートカット作成

$q = Read-Host "検索文字"
#$q = "デバッグ用"
$dirPath = "検索対象のディレクトリ"
$result = gci $dirPath -include *$q*.txt -Recurse -Force | Where-Object {$_.fullname -notlike "*\除外ディレクトリ\*"} | Sort-Object {$_.LastWriteTime} -Descending| Select-Object fullname,basename -first 1

$WshShell = New-Object -ComObject WScript.Shell
$ShortCut = $WshShell.CreateShortcut("./$($result.BaseName).lnk")
$ShortCut.TargetPath = $result.fullname
$ShortCut.Save()