content.php

Powershell Script – Get a list of all executable files in a directory

The customer had been through years of personnel changes that had resulted in thousands of files dumped into a shared directory. They wanted to inventory what executables they were supporting and make a plan for migrating them, but didn’t know whether they were dealing with tens, or thousands.

This script returns a list of every executable under the directory, and even has an “exclusion list” that allows you to specify directories you want to skip.

$parentDir = GetChildItem -Path \\ServerName\FolderName
$dirs = "\\ServerName\FolderName\"
$hash = @()

foreach($dir in $parentDir){
     $pathToCheck = $dirs + dir
     $exclude = 'FolderName1', 'FolderName2'
     if($exclude -contains [string]$dir){
         write-host "skipping " + $pathToCheck
     }else{
         $mt = Get-Children -Path $pathToCheck -Recurse
         foreach($t in $mt){
            #Comment out this write host line
            # for faster performance
            write-host "working on " + $t.BaseName $t.Extension
            if([string]$t.Extension -contains "exe"){
               $hash += New-Object PSObject -Property @{
                  'Path' = $pathToCheck + "\" + $t
                  'Filename' = $t.BaseName
               }
            }
         }
     }
}
$hash | Export-Csv "C:\Some\Location\Filename.csv" -NoTypeInformation -append

Leave a Reply

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