Powershell script to search domain PC for specific files and output specific results.

I am looking to create a Powershell script that will search computers in specified OU and a specified location for specified files, then output that to a csv with the computername, and the files found or not found.  I have tried finding multiple scripts that do some of what I wanted, and then merge them, with no avail. Red plagues my screen.

I am trying to understand how it functions as well, because I am not the best at this, and my knowledge can fit on the tip of a ball point pen with Powershell.

I found this one, and I cannot seem to tweak it to work, it searches, but wont write to the csv. I have added several filenames I know are in the PC, and still nothing. I am not sure If I have to format the text a specific way, etc.

Im sure there is a better way. If you wouldnt mind, could you explain the script briefly to me so I can learn instead of having to try and self learn.. Here is one of the examples I tried

EDIT: This does what i want, except I cannot alter it to search for specific OU groups, and I want it to say the computername file not found if it is not found.

ForEach ($system in (Get-Content C:\Users\PATHTOSYSTEMSFILE\systems.txt)) {
    # It's easier to have file path in a variable
    $dll = "\\$system\c`$\Windows\SysWOW64\msxml3.dll " #Can I use a wildcard here?
    $dll = "\\$system\c`$\Windows\SysWOW64\msxml6.dll "

    # Is the DLL there?    
    if  ( Test-Path  $dll){
        # Yup, get the version info
        $ver = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($dll).FileVersion
        # Write file path and version into a file.  
        Add-Content -path C:\Users\PATHTORESULTS\results.csv  "$dll,$ver"

Thank you for your assistance
Who is Participating?
Assumes that the file list contains one file name per line, with a full absolute path.
Nothing remarkable about it, except maybe lines 5-8 - the hash tables passed to the -Property argument of Select-Object are "calculated properties", that is, properties that don't exist on the current object in the pipeline, and will be added on the fly based on the expression ("e={...}").
$computers = Get-ADComputer -Filter * -Searchbase "ou=INSERTOUHERE,dc=DCHERE,dc=DCHERE" | Select-Object -ExpandProperty Name
$fileNames = Get-Content "C:\Users\PATHTOFILELIST\filenamelist.txt"
$computers | ForEach-Object {
	$computer = $_
	$fileNames | Select-Object -Property `
		@{n='ComputerName'; e={$computer}},
		@{n='FileName'; e={$_}},
		@{n='Found'; e={Test-Path -Path "\\$($computer)\$($_.Replace(':', '$'))"}}
} | Export-Csv -NoTypeInformation C:\Users\PATHTORESULTS\FoundFiles.csv

Open in new window

cstambaughAuthor Commented:
Hello thanks for the reply.
I was in the process of editing and saw it pop up when I was done. I tried thisversion, and it works, except says FALSE on all of the results, even if the file is there.
Then the list is probably in an incorrect format.
Based on "a specified location for specified files", the script expects full absolute path per line. No quotes (though that could be fixed if necessary), just the path.
C:\Program Files\Internet Explorer\iexplore.exe

Open in new window

ComputerName FileName                                        Found
------------ --------                                        -----
acme01       C:\Windows\explorer.exe                          True
acme01       C:\Program Files\Internet Explorer\iexplore.exe  True
acme01       C:\Windows\foo.bar                              False

Open in new window

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

cstambaughAuthor Commented:
Thank you It works great!
To run that against multiple OUs in one go, set $computers like this:
$OUList = @(
$computers = $OUList | ForEach-Object {Get-ADComputer -Filter * -Searchbase $_ | Select-Object -ExpandProperty Name}

Open in new window

cstambaughAuthor Commented:
Great! The next task is to figure out how to add file version information for future uses of this script. Thank you for the assist today.
cstambaughAuthor Commented:
Ok, I have been trying for a few days now to add other properties to the output of this script, like version info, modified date, etc, and cant seem to get it to work.

I am trying to add them here, in this part
      @{n='ComputerName'; e={$computer}},
		@{n='FileName'; e={$_}},
		@{n='Found'; e={Test-Path -Path "\\$($computer)\$($_.Replace(':', '$'))"}}

Open in new window

by adding another line with @{n='Propertywanted';  But Im not sure what goes afterwards, and why.
Am I going about it all wrong?
I want to add different properties to search for in the script, and understand how it works to adapt it for different situations, like when my boss asks if there is a certain program or version of a file, or when this was installed on this group or that group of PC's, without going to the individual machines.
I didnt want to open a new question, as I figured it wouldnt make much sense without the original content.
Thanks again
cstambaughAuthor Commented:
Basically I want to incorporate this into it somehow.

 [System.Diagnostics.FileVersionInfo]::GetVersionInfo("C:\Windows\Pathtofile") | Format-List -property *
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.

All Courses

From novice to tech pro — start learning today.