Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-07-23
11
Medium Priority
?
379 Views
Last Modified: 2014-07-24
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!
0
Comment
Question by:bbimis
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 40214729
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?
0
 
LVL 67

Assisted Solution

by:sirbounty
sirbounty earned 1000 total points
ID: 40214731
Good info here: stackoverflow.com/questions/7196937/how-to-speed-up-powershell-get-childitem-over-unc

# SCOPE: SEARCH A DIRECTORY FOR FILES (W/WILDCARDS IF NECESSARY)
# Usage:
# $directory = "\\SERVER\SHARE"
# $searchterms = "filname[*].ext"
# PS> $Results = Search $directory $searchterms

[reflection.assembly]::loadwithpartialname("Microsoft.VisualBasic") | Out-Null

Function Search {
  # Parameters $Path and $SearchString
  param ([Parameter(Mandatory=$true, ValueFromPipeline = $true)][string]$Path,
  [Parameter(Mandatory=$true)][string]$SearchString
  )
  try {
    #.NET FindInFiles Method to Look for file
    # BENEFITS : Possibly running as background job (haven't looked into it yet)

    [Microsoft.VisualBasic.FileIO.FileSystem]::GetFiles(
    $Path,
    [Microsoft.VisualBasic.FileIO.SearchOption]::SearchAllSubDirectories,
    $SearchString
    )
  } catch { $_ }

}

Open in new window

0
 

Author Comment

by:bbimis
ID: 40214773
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

0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
LVL 14

Expert Comment

by:Justin Yeung
ID: 40214798
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?
0
 

Author Comment

by:bbimis
ID: 40214917
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!
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 40214938
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.
0
 

Author Comment

by:bbimis
ID: 40214965
so can you help me with a solution that will scan all of them even if it errors?
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 40215025
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.
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 40215030
Are you able to narrow down the directories to search for?
0
 
LVL 14

Accepted Solution

by:
Justin Yeung earned 1000 total points
ID: 40215601
I agree with Qlemo, if you want  a script to scan the network will take ages to finish (it really do 1 by 1).....specially you are talking about scanning all machines on the network (I don't know how much machine you have but it is not fun even with 100 machines)

either setup a powershell script to run via GPO as logon script or enable powershell remoting (I personally disagree enabling PS remoting on workstation)
0
 

Author Closing Comment

by:bbimis
ID: 40216659
i used this method and worked fine after a few tweaks for my enviroment.  Thanks!
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
This Micro Tutorial will teach you the basics of configuring your computer to improve its speed. It will also teach you how to disable programs that are running in the background simultaneously. This will be demonstrated using Windows 7 operating…
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question