Solved

Powershell script to find files on network computers.

Posted on 2014-09-23
12
4,665 Views
1 Endorsement
Last Modified: 2016-11-01
I am in need of a Powershell script to locate specified files on network (domain) computers.  I need to determine the computer, size, and location of the file if found.  I have 40 files to look for and they are of varying type.  I do have the exact name for each file.

If necessary, I can run the script 40 times, but would prefer a more complete solution.

Thank you.
1
Comment
Question by:mobanker
12 Comments
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40339166
are you just searching in shares or or you trying to search the entire hard drive of every computer on the network?
0
 

Author Comment

by:mobanker
ID: 40339173
Searching the entire drive.  Drives C: and D: should cover everything.

Thank you!
0
 
LVL 16

Accepted Solution

by:
Joshua Grantom earned 500 total points
ID: 40339298
Here you go, This will take a very long time to run, I would run it from a server and leave it. Unfortunately there is no quick way to search every harddrive on a network. You will need to run this with a Domain Admin Account.

$computers = Get-ADComputer -filter *  | Select -Exp Name

$filenames = Get-Content "C:\filenamelist.txt"

foreach ($computer in $computers) {

foreach ($filename in $filenames) {
Get-ChildItem -Recurse -Force \\$computer\c$ -ErrorAction SilentlyContinue | Where-Object { ($_.PSIsContainer -eq $false) -and  ( $_.Name -eq "$filename") } | Select-Object Name,Directory| Export-Csv C:\FoundFiles.csv -nti -append
}
}

Open in new window


You need to add each file you are looking for on a single line in a text file name filenamelist.txt in your c drive. It will export a csv file on your C drive named FilesFound.csv
0
 

Author Comment

by:mobanker
ID: 40339314
A thing a beauty!  Thank you for your help Joshua.
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40339344
That was just for the C drive btw.

Just add this line below the other get-childitem

Get-ChildItem -Recurse -Force \\$computer\d$ -ErrorAction SilentlyContinue | Where-Object { ($_.PSIsContainer -eq $false) -and  ( $_.Name -eq "$filename") } | Select-Object Name,Directory| Export-Csv C:\FoundFiles.csv -nti -append

Open in new window

0
 

Author Comment

by:mobanker
ID: 40340060
Thank you, I am running it now.  I had a little issue with the c:\filenamelist.txt getting named c:\filename.txt.txt which threw some permission errors.  Once I fixed that, it is running and returning results properly.

I will add that line and re-start!
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40340111
This is a better version for future reference

$computers = Get-ADComputer -filter *  | Select -Exp Name

$filenames = Get-Content "C:\filenamelist.txt"

foreach ($computer in $computers) {

foreach ($filename in $filenames) {
Get-ChildItem -Recurse -Force \\$computer\c$, \\$computer\d$ -ErrorAction SilentlyContinue | Where-Object { ($_.PSIsContainer -eq $false) -and  ( $_.Name -eq "$filename") } | Select-Object Name,Directory| Export-Csv C:\FoundFiles.csv -nti -append
}
}

Open in new window

0
 

Author Comment

by:mobanker
ID: 40340125
It is running!  Have a great day and thank you very much.
0
 

Expert Comment

by:Member_2_7971328
ID: 41753011
Joshua,

I know this post is older, but is there any way you could add the last date modified to the output of that cleaner script?
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 41753291
Sure,

To add that you just need to add LastWriteTime to the end of the Select-Object like so.

Select-Object Name,Directory,LastWriteTime

This should include it in the CSV
0
 

Expert Comment

by:Junior Sofe
ID: 41837787
hi,

I am trying to write a PS script that searches for pst files. How can be that be done??
0
 

Expert Comment

by:Dmitry Klimenko
ID: 41868516
I write some script, but need full automatic list of all possible shares on computer (not like users shares, only system: c$, d$, e$, etc). How parse out of this command?
gwmi -class Win32_Share -computer comp1 | ft Name
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Synchronize a new Active Directory domain with an existing Office 365 tenant
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now