dlemley
dlemley

Reputation: 45

PowerShell script - check multiple PC's for file existence then get that file version

My PowerShell skills are in their infancy so please bear with me. What I need to do is take a list of PC's from a text file and check for file existence. Once that has been determined, I need to take those PC's that have the file and check the file for its FileVersion. Then in the end, output that to a CSV file.

Here's what I have and I'm not really sure if this is even how I should be going about it:

ForEach ($system in (Get-Content C:\scripts\systems.txt))

if  ($exists in (Test-Path \\$system\c$\Windows\System32\file.dll))
{
    Get-Command $exists | fl Path,FileVersion | Out-File c:\scripts\results.csv -Append
}   

Upvotes: 3

Views: 19989

Answers (1)

vonPryz
vonPryz

Reputation: 24071

Not bad for a starter script, you got it almost right. Let's amend it a bit. To get the version info we'll just get a working code from another an answer.

ForEach ($system in (Get-Content C:\scripts\systems.txt)) {
    # It's easier to have file path in a variable
    $dll = "\\$system\c`$\Windows\System32\file.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:\scripts\results.csv "$dll,$ver"
    }
}

Upvotes: 5

Related Questions