使用PowerShell遍历计算机上的驱动器

时间:2018-04-01 09:23:26

标签: powershell

尝试在计算机上搜索.txt文件中的所有驱动器,但不包含一个目录,但结果证明不准确

$drv = get-psdrive ForEach ($drive in $drv) { gci -include *.txt -exclude excludeDir -recurse }

1 个答案:

答案 0 :(得分:0)

cls

function isExclude($f){

    $excludes= @("C:\myExclude", "C:\`$Recycle.Bin");

    $excludes |%{
        if($f.StartsWith($_)){
            return $true;
        }
    }

    return $false
}


function isSearch($f){

    $searchs= @(".txt");

    $searchs |%{
        if($f.EndsWith($_)){
            return $true;
        }
    }

    return $false
}



try{            

    get-psdrive|% {         

         if(-not [String]::IsNullOrEmpty($_.Root) -and -not $_.Root.StartsWith("\") -and -not $_.Root.StartsWith("HKEY")){
            $r = $_.Root

            Write-host -ForegroundColor black ("Checking "+$r+" drive for txt files")

            Get-ChildItem -Path $r -Recurse -Force -ErrorAction SilentlyContinue |%{

                #$_.FullName                                              

                if(-not (isExclude($_.FullName)) -and (isSearch($_.FullName))){
                    write-host -ForegroundColor Gray ($_.FullName )
                }
            }
          }
     }

}catch{
    $_
}

注意:在GCI中使用-Filter * .txt,也找到以“.txtxxx”结尾的文件