Tuesday, January 16, 2018

Powershell script to find all exes in Downloads folder

$erroractionPreference="stop"
$pcs = Get-ADComputer -Filter "Name -like '*'" | select-object name
$pccount = $pcs.Count
$counter = 1
foreach ($pc in $pcs) {
    $computername = $pc.name
    if(Test-Connection -ComputerName $computername -Count 1 -Quiet){
        Try
        {
            $path = "\\" + $computername + "\c$\users"
            $users = Get-ChildItem -Path $path
            foreach($user in $users){
                $downloads = $path + "\" + $user + "\Downloads"
                Write-Host $counter" of "$pccount" Searching "$downloads
                $downloadexes = Get-ChildItem $downloads -Filter *.exe | select fullname
                foreach($file in $downloadexes){
                    $filepath = $file.FullName
                    $hashes = Get-FileHash -Algorithm MD5 $filepath | select Path, Hash
                    foreach($hash in $hashes){
                        $result = $computername + "`t" + $hash.Hash + "`t" + $hash.Path
                        Write-Host $result
                    }
                }
                $downloadmsis = Get-ChildItem $downloads -Filter *.msi | select fullname
                foreach($file in $downloadmsis){
                    $filepath = $file.FullName
                    $hashes = Get-FileHash -Algorithm MD5 $filepath | select Path, Hash
                    foreach($hash in $hashes){
                        $result = $computername + "`t" + $hash.Hash + "`t" + $hash.Path
                        Write-Host $result
                    }
                }
            }
        }
        Catch
        {
        Write-Host $counter" of "$pccount" Search Error "$computername
        }
    }
    else{
        Write-Host $counter" of "$pccount" Search Connection Failure "$computername
    }
    $counter++

}

No comments:

Post a Comment