Link to home
Start Free TrialLog in
Avatar of bbimis
bbimis

asked on

what would be the fastest way in powershell or batch to search for the existance of a file

what would be the fastest way to search the entire computer for a known file and make a log of what computers have the file?
basically search c:\*.* recursive for somefile.exe

I need to see if this file being used on our network because it needs to be removed.  Thanks!
Avatar of Justin Yeung
Justin Yeung
Flag of United States of America image

do you mean search the entire network of all the computers that contain certain .exe file and create a log of what is found and not?
SOLUTION
Avatar of sirbounty
sirbounty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bbimis
bbimis

ASKER

something more on the lines of this
$creds = Get-Credential
Get-Content C:\office.txt | ForEach-Object `
{
    $compname = $_
    If (Test-Connection $compname -Count 1 -Quiet)
    {
      $files = get-childitem c:\ -recurse | ? {$_.name -eq "blah.exe"}

 	  write-host there are $files.count matching that name on computer $compname

 	  foreach ($file in $files){
 		$directory = $file.directory
 		$fulldirectorypath = $file.directory.fullname
 		$fullfilepath = $file.fullname
		
		$info1 = $file.name 
        $info2 = "The directory path is $fulldirectorypath "
        $info3 = "Computer ID is: $compname "
        Write-Host $info3
		"---------------------------" | Out-File c:\blah.txt -Append
		$info1|Out-File c:\blah.txt -Append
 		$info2|Out-File c:\blah.txt -Append 
		$info3|Out-File c:\blah.txt -Append
		"*******End of Data*************" | Out-File blah.txt -Append
      }
        } 
    }

Open in new window

ok, so how would you get the source? from AD search with specified name? or from an OU?
or from a txt file?

and the output, what info would you need?

what would you like to do with the file that found? delete it ? move it somewhere and rename it?
Avatar of bbimis

ASKER

Well we have found a file that is causing errors on our system. i would simply like to generate a list and make 100% certain that i have scanned each computer on the network with the supplied list (i pull it form active directory).  
i would like the file to be in cvs or excel so i could look at each computer in a grid and see if it has the file and also where on the system it is.

Thanks for the help!
Ok, so you have a text list with computer names we can process. No issue with that.

Get-ChildItem is not suited well, unless execute locally. It needs to create and manage a lot of objects, so a lot of overhead there. You do not really need the file info, only path and name, and that can be retrieved with the above routine.
However,
a) it is processing one machine at a time, waiting for completion
b) stops as soon as there is an error like missing privileges.
Avatar of bbimis

ASKER

so can you help me with a solution that will scan all of them even if it errors?
Sadly I cannot see anything different than either running Get-ChildItem with an UNC path  - slow, involving a lot of resources, ... -  or remote execute that cmdlet. However, that requires to have PowerShell Remoting enabled on all boxes, and set up correctly so you can access it. Not something you can be sure about.

The traditional approach is to write a logon script running the scan in background locally, writing back results to a central file, and not running if it has been running already.
Are you able to narrow down the directories to search for?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bbimis

ASKER

i used this method and worked fine after a few tweaks for my enviroment.  Thanks!