Powershell to find if a file exist on 20 servers

Hi All,

I have about 20 Windows OS servers and I need a powershell script that read a text file that contains a list of server names, then go into each server and find out if a particular file exists, if it does, write the host name to fileexsit.txt and it the file doesn't exist, write to a file named filenotexist.txt.

Please help!!

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

This will use the UNC and create three files, the third one for files where the network share wasn't accessible (server turned off, firewall issue, permission issue, ...).
$localFilePath = 'C:\Temp\FindMe.txt'
$fileExists = 'C:\Temp\fileexist.txt'
$fileMissing = 'C:\Temp\filemissing.txt'
$fileError = 'C:\Temp\fileerror.txt'

$fileExists, $fileMissing, $fileError | Remove-Item ErrorAction SilentlyContinue
$drive = Split-Path -Path $localFilePath -Qualifier
Get-Content -Path C:\Temp\servers.txt | ForEach-Object {
	Write-Host "Processing $($_)"
	$share = "\\$($_)\$($drive.Replace(':', '$'))"
	If (Get-Item -Path $share -ErrorAction SilentlyContinue) {
		If (Test-Path "\\$($_)\$($localFilePath.Replace(':', '$'))") {
			$_ | Add-Content -Path $fileExists
		} Else {
			$_ | Add-Content -Path $fileMissing
	} Else {
		"$($_): Unable to access '$($share)'!" | Add-Content -Path $fileError

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Something along these lines. Note that myhostname is a fqdn list of hosts one for each row (no header)
I’m writing from memory so syntax may be imprecise.
  $filename = C:\filename
Get-content myhostnames.txt | % {

$result =    Invoke-command -computername $_  -scriptblock { if ( test-file $using:filename ) { $true} else { $false } }
If ( $result ) { $_ >> fileexists.txt} else { $_.name >> fileNOTexists.txt}
 test-path with test-file... cannot edit from mobile sorry
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.