I wrote a little function to scan each folder in $PSModulePath to see if duplicate folder names exist in the various paths in there (as I've found this problem happening quite often in my PowerShell environments!). I use simple logic and I was wondering if some PowerShell gurus maybe have more compact / faster / more efficient ways to achieve a sweep like this (I quite often find that those better at PowerShell seem to have 2-line solutions to something that takes me 15 lines! :-) )?
I'm just taking a path in $PSModulePath and creating an array of the subfolder names there, then looking at the subfolders of the other paths in $PSModulePath and comparing them one by one against the array that I made for the first path, and then repeating for the other paths.
function Find-ModuleDuplicates {
$hits = ""
$ModPaths = $env:PSModulePath -Split ";" -replace "\\+$", "" | sort
foreach ($i in $ModPaths) {
foreach ($j in $ModPaths) {
if ($j -notlike "*$i*") {
$arr_i = (gci $i -Dir).Name
$arr_j = (gci $j -Dir).Name
foreach ($x in $arr_j) {
if ($arr_i -contains $x) {
$hits += "Module '$x' in '$i' has a duplicate`n"
}
}
}
}
}
if ($hits -ne "") { echo "" ; echo $hits }
else { "`nNo duplicate Module folders were found`n" }
}