Find a file in an array of computers

# Define the array of computer names
$computers = @("Computer1", "Computer2", "Computer3")

# Define the file name or pattern you want to search for (with wildcards if needed)
$targetFileName = "*password*.txt"

# Define the directory where you want to start the recursive search on each computer
$targetDirectory = "C$"

# Loop through each computer and search for the file
foreach ($computer in $computers) {
    $filePath = "\\$computer\$targetDirectory"

    # Get all files with the specified name/pattern in the target directory and its subdirectories
    $foundFiles = Get-ChildItem -Path $filePath -Filter $targetFileName -Recurse -ErrorAction SilentlyContinue

    # Check if any file was found
    if ($foundFiles) {
        foreach ($file in $foundFiles) {
            Write-Host "File '$($file.Name)' found on $computer at $($file.FullName)"
        }
    } else {
        Write-Host "File '$targetFileName' not found on $computer"
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *