• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1987
  • Last Modified:

Need assistance with a PowerShell script used to find ALL .exe files on the C: drive of multiple computers

I would like to use PowerShell or VBScript to find all .exe files on the C: drive of multiple computers then write the path and file to a specified text output file. Only the C: drive and all folders, including the root.

I've started with the following script but it seems to repeat what it found on the first computer for all subsequent computers. Can this be done in PowerShell AND VBScript if it isn't too big a project?

Thanks in advance for your help!!

Below is the PowerShell Script:

# PowerShell script to list specified file types in any folder / any drive

$strComputers = Get-Content -Path "C:\Test\ServerList.txt"
foreach($strComputer in $strComputers)
{
$Dir = get-childitem C:\ -recurse
$List = $Dir | where {$_.extension -eq ".exe"}
# the "- Width 200" statement below can be increased if you have Long Paths
$List |ft fullname -wrap |out-file C:\Test\dircom.txt -Width 200 -append
}


 
0
lasthope
Asked:
lasthope
  • 6
  • 5
  • 5
  • +1
2 Solutions
 
RobSampsonCommented:
Hi, I don't have time to do it at the moment, but the immediate problem I see is that it's only going to search your local computer.  Change this:
$Dir = get-childitem C:\ -recurse

to this
$Dir = get-childitem \\$_\C$\ -recurse

and it should then search the remote C$ share.

I also think VBScript would be too slow....if I were to do it in VBScript, I'd just shell out to the DOS dir command anyway....

Regards,

Rob.
0
 
lasthopeAuthor Commented:
Gave the suggested change a quick try. Below are the error messages.

Get-ChildItem : The UNC path should be of the form \\server\share.
At C:\TEST\ListFiles(Use This One).ps1:7 char:21
+ $Dir = get-childitem <<<<  \\$_\C$\ -recurse
    + CategoryInfo          : InvalidArgument: (\\\C$\:String) [Get-ChildItem], ArgumentException
    + FullyQualifiedErrorId : ItemExistsArgumentError,Microsoft.PowerShell.Commands.GetChildItemCommand
 
Get-ChildItem : Cannot find path '\\\C$\' because it does not exist.
At C:\TEST\ListFiles(Use This One).ps1:7 char:21
+ $Dir = get-childitem <<<<  \\$_\C$\ -recurse
    + CategoryInfo          : ObjectNotFound: (\\\C$\:String) [Get-ChildItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
0
 
YZlatCommented:
you could do something like that in VBScript:

 
On Error Resume Next

'open the file system object
Set FSO = CreateObject("Scripting.FileSystemObject")
set WSHShell = wscript.createObject("wscript.shell")

Set Stream = FSO.OpenTextFile("C:\Test\ServerList.txt")
computers = Split(Stream.ReadAll, vbNewLine)
Stream.Close

OutputFilePath="C:\Test\Output.txt"
Set oReportFile = FSO.CreateTextFile(OutputFilePath, True)

For Each computer In computers
	Set objWMIService = GetObject("winmgmts:\\" & computer & "\root\cimv2")

	Set colFiles = objWMIService. _
    	ExecQuery("Select * From CIM_DataFile Where Extension = 'exe'")

	If colFiles.Count = 0 Then
    		Wscript.Echo "no exe files found"
	Else
		For Each oFile in colFiles
			oReportFile.WriteLine(oFile.FileName)
    		Next
	End If
Next
Set FSO=nothing

Open in new window

0
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

 
YZlatCommented:
With PowerShell you can try something like that:

 
$files = get-wmiobject -query "Select * from CIM_DataFile Where Extension = 'exe'" -ComputerName (Get-Content C:\Test\ServerList.txt) 

foreach($file in $files)
{

}

Open in new window

0
 
lasthopeAuthor Commented:
YZlat - I have a VBscript that will do the list similar to the one you offered here.  Only problem is with the "\root\cimv2" the script finds .exe files on ALL drives.  I only want the specified drive, in this case C:

What can be done to check only a specified hard drive?
 
0
 
YZlatCommented:
try

$files = get-wmiobject -query "Select * from CIM_DataFile Where Extension = 'exe'" -ComputerName (Get-Content C:\Test\ServerList.txt) 

| out-File "C:\Test\Output.txt"

Open in new window


or

 
$files = get-wmiobject -query "Select * from CIM_DataFile Where Extension = 'exe'" -ComputerName (Get-Content C:\Test\ServerList.txt) 

foreach($file in $files)
{
	$file.name out-File "C:\Test\Output.txt"
}

Open in new window

0
 
YZlatCommented:
you can specify Drive and/or Path in this query:

Select * From CIM_DataFile Where Extension = 'exe'

like

Select * From CIM_DataFile Where Extension = 'exe' AND Drive="..."
0
 
YZlatCommented:

 
On Error Resume Next

'open the file system object
Set FSO = CreateObject("Scripting.FileSystemObject")
set WSHShell = wscript.createObject("wscript.shell")

Set Stream = FSO.OpenTextFile("C:\Test\ServerList.txt")
computers = Split(Stream.ReadAll, vbNewLine)
Stream.Close

OutputFilePath="C:\Test\Output.txt"
Set oReportFile = FSO.CreateTextFile(OutputFilePath, True)

For Each computer In computers
	Set objWMIService = GetObject("winmgmts:\\" & computer & "\root\cimv2")

	Set colFiles = objWMIService. _
    	ExecQuery("Select * From CIM_DataFile Where Extension = 'exe' AND Drive='c:' AND Path='Test'")

	If colFiles.Count = 0 Then
    		Wscript.Echo "no exe files found"
	Else
		For Each oFile in colFiles
			oReportFile.WriteLine(oFile.FileName)
    		Next
	End If
Next
Set FSO=nothing

Open in new window

0
 
lasthopeAuthor Commented:
YZlat

The PowerShell script works but it again gives ALL drives. I would like to only get results from C:.

The VBScript works but doesn't include the "path" in the output.  Any idea what to change?

I think the VBScript is going to be too slow. I'm testing on 2 machines but I have over 120 to run this report for.  VBScript is great for many things but I think I'll pass here and stick with PowerShell.
0
 
chrismerrittCommented:
Try this in your PowerShell query:

Get-WmiObject -Query "SELECT * From CIM_DataFile WHERE Name LIKE '%.exe' AND Name LIKE 'C:\\%'"
0
 
RobSampsonCommented:
Here's one option, just a slight change to your original code:
$strComputers = Get-Content -Path "C:\Test\ServerList.txt"
foreach($strComputer in $strComputers)
{
Write-Host "Scanning \\$strComputer\C$\"
$Files = gci "\\$strComputer\C$\" -recurse | Where {$_.extension -eq ".exe"}
$Files | ft fullname -wrap|out-file C:\Test\dircom.txt -Width 200 -append
}

Open in new window


Rob.
0
 
RobSampsonCommented:
IMO, the CIM_DataFile query takes too long, even if you use
And Drive = 'C:'

to restrict the search to local drives.

Rob.
0
 
lasthopeAuthor Commented:
Sorry ALL.  I will give these a try, but I'm getting, and giving, a moving target.  Now I need to find .exe files on ALL server hard drives and output to the same text file.  One other important thing, the output must include the path to the .exe.

Thanks again and sorry about the moving target.
0
 
lasthopeAuthor Commented:
Rob - I agree with you that the CIM_Datafile query takes too long.  And I have 120 servers with multiple drives to search.
0
 
RobSampsonCommented:
OK, this should do the job on all logical drives of each server.

Regards,

Rob.
foreach($disk in Get-WmiObject Win32_LogicalDisk -filter "DriveType=3" -computer (Get-Content "C:\Test\ServerList.txt"))
{
	$remotepath = "\\$($disk.SystemName)\$($disk.DeviceID.Replace(':','$'))\"
	Write-Host "Scanning $remotepath"
	$Files = Get-ChildItem "$remotepath" -recurse | Where {$_.extension -eq ".exe"}
	$Files | ft fullname -wrap|out-file C:\Test\dircom.txt -Width 200 -append
}

Open in new window

0
 
lasthopeAuthor Commented:
Each of these scripts works to the extent of security on our systems. Biggest barrier is no administrative shares, so scripts searching C$, D$ so forth cannot be used. If it were an easy script to change autoshareserver registry key to 1 and back to 0 with the rest of the code sandwiched inbetween this may have been a whole lot easier on me, and possibly faster to complete. As it is it was a long excercise, but the results appear to be accurate.

Thank you for your expert assistance.
0
 
RobSampsonCommented:
We didn't know you had admin shares disabled.....in that case WMI is your only option.  One option though, if WMI does work, is that you could use WMI to *create* a share on the root of the drive, then do the search, and remove the share, but you seem to have moved on anyway.

Glad we could help.

Rob.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

  • 6
  • 5
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now