<

PowerShell Script to check file version across the network

Published on
3,628 Points
528 Views
1 Endorsement
Last Modified:
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.

Our office runs an application that is constantly being updated.  Unfortunately the program is not easy to update via Group Policy (GPO), so instead of walking to each computer to check if it needs an upgrade or if it is running multiple versions, I wrote this script to do the legwork for me.  To modify this script simply change <ProgramVersions> <ProgramFolder> and <ProgramFile> to suit your needs.  You will also need to modify the  $LogFile variable to a path that exists on your own system.  It can take some time to run, especially if you have a lot of systems to check.  The end result is a log file that tells you where to find what you are looking for so you can work more efficiently.


# I like a tidy workspace

clear

# Errors happen, just move on.  Comment this out if you want to see what broke.

$ErrorActionPreference = "silentlycontinue"

# Make a log,  

$LogTime = Get-Date -Format "MM-dd-yyyy_hh.mm.ss"

$LogFile= 'C:\work\<ProgramVersions>.'+$logtime+'.txt'

$logtime | Out-File $LogFile

# Need this to discover what computers are on the network

Import-Module ActiveDirectory

$computer = (Get-ADComputer -Filter 'ObjectClass -eq "Computer"' | Select -Expand DNSHostName)

$drawline=  "-----------------------------------------------------------------------------------------------------------------------"

# Get that list of computers and do something with it.

foreach ($computer1 in $computer) {

# Define what we are looking for

$path="\\$computer1\c$\<ProgramFolder>*\"

$filename="<Programfile>"

$FilePath=$path+$filename

# The program we are looking for may have multiple instances.  Here's another possible location.

$path2="\\$computer1\c$\<ProgramFolder>*\"

$filename2="<Programfile>"

$FilePath2=$path2+$filename2

# Go find what we are looking for and log it.

if ((Get-ChildItem -Recurse $FilePath) -eq $null){

    Out-Null

    }

else {

$drawline | Tee-Object $LogFile -Append

$computer1 | Tee-Object $LogFile -Append

Get-ChildItem  -Recurse $FilePath | foreach-object { "{0,-80}`t{1}" -f $_.FullName,  [System.Diagnostics.FileVersionInfo]::GetVersionInfo($_).FileVersion } |  Tee-Object $LogFile -Append

Get-ChildItem -Recurse  $FilePath2 | foreach-object { "{0,-80}`t{1}" -f $_.FullName,  [System.Diagnostics.FileVersionInfo]::GetVersionInfo($_).FileVersion }   | Tee-Object $LogFile -Append

$drawline| Tee-Object $LogFile -Append

}

}

}

The way this works is pretty simple.  We first ask Active Directory for a list of computers (this is why it's important to keep active directory tidy folks).  Then the script goes around an asks every computer in that list to search it's file system to see if they have this program installed.  In my case, I know that the program I am looking for is in multiple locations, so ask again for the other location(s).  Then give me a report on what you found.


It is possible to modify this script to find the installed program file in the registry, even multiple instances, but then it will not find those programs that do not make registry entries.  Also I wanted this script to be flexible enough that I could look for any file, not just programs.


I try to keep my code as simple and readable as possible.  There is a difference between code and cryptogrophy.  If you make suggestions to improve this code, please document it.  It really helps, even the experienced.


This is a reprint of my original article on my blog xpertnotes.net.  Click here to see the original article.

1
Comment
2 Comments
 
LVL 8

Expert Comment

by:Senior IT System Engineer
Does the user will notice any performance issue when the scanning is running ?
0
 
LVL 9

Author Comment

by:Frank McCourry
Performance degradation is relative only to the number of paths and at what level you enter that path.  It is the equivalent of performing a non-indexed search on the users computer.  If you keep the search path narrow, the user will probably not even notice.  If you start at the root, I guarantee that your phone will ring.  

This script was designed to find a specific file in specific locations, thereby limiting the amount of hard drive activity it causes on the end users computer.  I run this once a month or so and have had no complaints.
0

Featured Post

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.

Join & Write a Comment

The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month