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

Hello,
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
cstambaughAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

oBdACommented:
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

1

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
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.
1
oBdACommented:
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.
Filenames:
C:\Windows\explorer.exe
C:\Program Files\Internet Explorer\iexplore.exe
C:\Windows\foo.bar

Open in new window

Result:
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

1
Make Network Traffic Fast and Furious with SD-WAN

Software-defined WAN (SD-WAN) is a technology that determines the most effective way to route traffic to and from datacenter sites. Register for the webinar today to learn how your business can benefit from SD-WAN!

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

Open in new window

2
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.
0
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
1
cstambaughAuthor Commented:
Basically I want to incorporate this into it somehow.

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

From novice to tech pro — start learning today.