Skip to content

Commit

Permalink
Refactor application folder enumeration
Browse files Browse the repository at this point in the history
  • Loading branch information
itm4n committed Dec 28, 2024
1 parent b86e254 commit 746f50d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 31 deletions.
1 change: 1 addition & 0 deletions info/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Changed

- Add multithreading to application file permissions check.
- Refactor application folder enumeration.

## 2025-12-27

Expand Down
62 changes: 31 additions & 31 deletions src/helper/Environment.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -55,51 +55,51 @@ function Get-InstalledApplication {
[switch] $Filtered = $false
)

$IgnoredPrograms = @("Common Files", "Internet Explorer", "ModifiableWindowsApps", "PackageManagement", "Windows Defender", "Windows Defender Advanced Threat Protection", "Windows Mail", "Windows Media Player", "Windows Multimedia Platform", "Windows NT", "Windows Photo Viewer", "Windows Portable Devices", "Windows Security", "WindowsPowerShell", "Microsoft.NET", "Windows Portable Devices", "dotnet", "MSBuild", "Intel", "Reference Assemblies")
begin {
$IgnoredPrograms = @( "Common Files", "Internet Explorer", "ModifiableWindowsApps", "PackageManagement", "Windows Defender", "Windows Defender Advanced Threat Protection", "Windows Mail", "Windows Media Player", "Windows Multimedia Platform", "Windows NT", "Windows Photo Viewer", "Windows Portable Devices", "Windows Security", "WindowsPowerShell", "Microsoft.NET", "Windows Portable Devices", "dotnet", "MSBuild", "Intel", "Reference Assemblies" )
}

$InstalledPrograms = New-Object System.Collections.ArrayList
process {
$InstalledPrograms = @()
$InstalledPrograms += Get-ChildItem -Path $(Join-Path -Path $env:SystemDrive -ChildPath "Program Files (x86)") -ErrorAction SilentlyContinue | Where-Object { $_ -is [System.IO.DirectoryInfo] }
$InstalledPrograms += Get-ChildItem -Path $(Join-Path -Path $env:SystemDrive -ChildPath "Program Files") -ErrorAction SilentlyContinue | Where-Object { $_ -is [System.IO.DirectoryInfo] }

# List all items in 'C:\Program Files' and 'C:\Program Files (x86)'
$PathProgram32 = Join-Path -Path $env:SystemDrive -ChildPath "Program Files (x86)"
$PathProgram64 = Join-Path -Path $env:SystemDrive -ChildPath "Program Files"
$InstalledProgramsRegKeys = @()
$InstalledProgramsRegKeys += Get-ChildItem -Path "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" -ErrorAction SilentlyContinue
$InstalledProgramsRegKeys += Get-ChildItem -Path "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall" -ErrorAction SilentlyContinue

$Items = Get-ChildItem -Path $PathProgram32,$PathProgram64 -ErrorAction SilentlyContinue
if ($Items) {
[void] $InstalledPrograms.AddRange($Items)
}
foreach ($InstalledProgramsRegKey in $InstalledProgramsRegKeys) {

$RegInstalledPrograms = Get-ChildItem -Path "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
$RegInstalledPrograms6432 = Get-ChildItem -Path "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall" -ErrorAction SilentlyContinue
if ($RegInstalledPrograms6432) { $RegInstalledPrograms += $RegInstalledPrograms6432 }
$InstallLocation = [System.Environment]::ExpandEnvironmentVariables($InstalledProgramsRegKey.GetValue("InstallLocation"))
if ([String]::IsNullOrEmpty($InstallLocation)) { continue }
if (-not (Test-Path -Path $InstallLocation -ErrorAction SilentlyContinue)) { continue }

foreach ($InstalledProgram in $RegInstalledPrograms) {
$InstallLocation = $InstallLocation.Trim('\')

$InstallLocation = [System.Environment]::ExpandEnvironmentVariables($InstalledProgram.GetValue("InstallLocation"))
$FileItem = Get-Item -Path $InstallLocation -ErrorAction SilentlyContinue
if ($null -eq $FileItem) { continue }
if ($FileItem -isnot [System.IO.DirectoryInfo]) { continue }

if (-not [String]::IsNullOrEmpty($InstallLocation)) {
$InstalledPrograms += $FileItem
}

if (Test-Path -Path $InstallLocation -ErrorAction SilentlyContinue) {
foreach ($InstalledProgram in $($InstalledPrograms | Sort-Object -Property "FullName" -Unique)) {

if ($InstallLocation[$InstallLocation.Length - 1] -eq "\") {
$InstallLocation = $InstallLocation.SubString(0, $InstallLocation.Length - 1)
}
# Make sure we skip empty paths.
if ([string]::IsNullOrEmpty($InstalledProgram.FullName)) { continue }

$FileObject = Get-Item -Path $InstallLocation -ErrorAction SilentlyContinue -ErrorVariable GetItemError
if ($GetItemError) { continue }
# Make sure we don't treat system paths such as 'C:\Windows\System32' as
# application folders.
if (Test-IsSystemFolder -Path $InstalledProgram.FullName) { continue }

if (-not ($FileObject -is [System.IO.DirectoryInfo])) { continue }
# If the 'Filtered' switch is used, only return non-default application
# folder entries.
if ($Filtered -and ($IgnoredPrograms -contains $InstalledProgram.Name)) { continue }

[void] $InstalledPrograms.Add([Object] $FileObject)
}
# Keep only the folder name and full path.
$InstalledProgram | Select-Object -Property Name,FullName
}
}

foreach ($InstalledProgram in $($InstalledPrograms | Sort-Object -Property "FullName" -Unique)) {
if ([string]::IsNullOrEmpty($InstalledProgram.FullName)) { continue }
if (Test-IsSystemFolder -Path $InstalledProgram.FullName) { continue }
if ($Filtered -and ($IgnoredPrograms -contains $InstalledProgram.Name)) { continue }
$InstalledProgram | Select-Object -Property Name,FullName
}
}

function Get-SccmCacheFolderFromRegistry {
Expand Down

0 comments on commit 746f50d

Please sign in to comment.