Recently I was given the task to find how many computers on my network had a particular file, this is the PowerShell script I used to tackle it. Administrative access on all hosts is assumed. This script will work to find any file or directory from a list of host names contained in a txt. Once it finds the file on a host, it will then output the host name to a “Found file on host” txt. Should the host be offline or unavailable, that host name is put in an “Offline” txt for later review. Host names that do not have the file are disregarded.
To run the script:
- Make a directory and place the ps1 in that directory
- Make a hosts.txt file with the host names you want to run the script against, one host name per line. Note: Active Directory can export the content of an OU to a txt file.
- Next edit the highlighted paths (see below) to match your environment.
- Run the script
Download a zip of the script:
Remote_File_Query.zip (1 KB)
Code:
##########################################################
#
# Powershell Remote File Query
# Author RobWillis.info (2012)
#
####################################################################################################################
# Edit these variables to fit your enviroment
##########################################################
# Set file to be tested for, put everything after c:\
# “c:\Users\Default” is the example path
$filetofind = ‘Users\Default‘
# Hostnames TXT Location
$hostnamestxt = ‘C:\test\hosts.txt‘
# Destination file for Online Machines
$onlinetxt = ‘C:\test\Machines_with_file.txt‘
# Destination file for Offline Machines
$offlinetxt = ‘C:\test\Offline_Machines.txt‘##########################################################
# Begin Executing Script – Do Not Edit Below This Line
##########################################################
$computers = get-content “$hostnamestxt”write-host “———————————————-”
write-host “Scanning hostnames from $hostnamestxt…”
write-host “———————————————-”foreach($computer in $computers)
{
ping -n 1 $computer >$null
if($lastexitcode -eq 0)
{
if(test-path “\\$computer\c$\$filetofind”)
{
echo “$computer” | Out-File -Append “$onlinetxt”
write-host “File FOUND on $computer”
}
else
{write-host “File NOT found on $computer”}
}
else
{
echo “$computer” | Out-File -Append “$offlinetxt”
write-host “$computer is OFFLINE/DID NOT RESPOND TO PING”
}
}
write-host “———————————————-”
write-host “Script has completed please check output.”
write-host “Hosts with file output location – $onlinetxt”
write-host “Hosts that were unpingable output location – $offlinetxt”
write-host “———————————————-“
Output on screen:
———————————————-
Scanning hostnames from C:\test\hosts.txt…
———————————————-
File NOT found on PS3
192.168.1.95 is OFFLINE/DID NOT RESPOND TO PING
File FOUND on 127.0.0.1
File NOT found on esxi-w7
Fake is OFFLINE/DID NOT RESPOND TO PING
File NOT found on D420
File FOUND on Desktop
File NOT found on 192.168.1.202
Dell-Laptop is OFFLINE/DID NOT RESPOND TO PING
———————————————-
Script has completed please check output.
Hosts with file output location – C:\test\Machines_with_file.txt
Hosts that were unpingable output location – C:\test\Offline_Machines.txt
———————————————-