The 'Get-Command' or "where.exe' are too restrictive, they can't be use for all programms ...
Bellow 2 functions that work fine, one to get ShortCut informations, one to get Application information that I use in all my scripts :
Function Get-ShortCut {
param (
[string]$StringToSearch,
[array]$Directories = @(
"${Env:PROGRAMDATA}\Microsoft\Windows\Start Menu\Programs"
"${Env:APPDATA}\Microsoft\Windows\Start Menu\Programs"
),
[string]$TypeTargetFile = "exe"
)
$ShortCutArray = @()
$shellObject = New-Object -ComObject WScript.Shell
foreach ($Directory in $Directories) {
$shortcuts = Get-ChildItem -Path $Directory -Recurse -Filter *.lnk
foreach ($shortcut in $shortcuts) {
$ShortcutObject = $shellObject.CreateShortcut($shortcut.FullName)
if (($ShortcutObject.TargetPath -like "*$StringToSearch*.$TypeTargetFile") -or ($shortcut.BaseName -like "*$StringToSearch*")) {
$ShortCutArray += [PSCustomObject]@{
Name = $shortcut.BaseName
Path = $shortcut.FullName
TargetPath = $ShortcutObject.TargetPath
Arguments = $ShortcutObject.Arguments
WorkingDirectory = $ShortcutObject.WorkingDirectory
IconLocation = $ShortcutObject.IconLocation
Description = $ShortcutObject.Description
WindowStyle = $ShortcutObject.WindowStyle
Hotkey = $ShortcutObject.Hotkey
}
}
}
}
Return $ShortCutArray
}
}
Function Get-Application {
param (
[string]$StringToSearch
)
$hives = @(
"HKLM"
"HKCU"
)
$RegistryKeys = @(
"\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
"\SOFTWARE\Wow6432node\Microsoft\Windows\CurrentVersion\Uninstall"
)
$ApplicationArray = @()
ForEach ($hive in $hives) {
ForEach ($RegistryKey in $RegistryKeys) {
if (Test-Path "${hive}:${RegistryKey}" -ErrorAction SilentlyContinue) {
$Applications = Get-ChildItem "${hive}:${RegistryKey}" | ForEach-Object { Get-ItemProperty $_.PsPath } | Select DisplayName,DisplayVersion,InstallDate,UninstallString | Where-Object {$_.DisplayName -match "$StringToSearch"}
ForEach ($Application in $Applications) {
$DisplayName = $Application.DisplayName
$DisplayVersion = $Application.DisplayVersion
$UninstallString = $Application.UninstallString -Replace "`"",""
$InstallDate = $Application.InstallDate
$ApplicationShortCuts = Get-ShortCut $StringToSearch
if ($ApplicationShortCuts.Count -gt 1) {
$WorkingDirectory = $ApplicationShortCuts.WorkingDirectory[0]
$TargetPath = $ApplicationShortCuts.TargetPath[0]
$Arguments = $ApplicationShortCuts.Arguments[0]
} else {
$WorkingDirectory = $ApplicationShortCuts.WorkingDirectory
$TargetPath = $ApplicationShortCuts.TargetPath
$Arguments = $ApplicationShortCuts.Arguments
}
if ((-not $WorkingDirectory) -and $TargetPath) {$WorkingDirectory = Split-Path -Path $TargetPath -Parent}
$ApplicationArray += [PSCustomObject]@{
Name = $DisplayName
Version = $DisplayVersion
InstallDate = $InstallDate
Directory = $WorkingDirectory
Command = $TargetPath
Arguments = $Arguments
UninstallCommand = $UninstallString
}
}
}
}
}
if (-not $ApplicationArray) {
$ApplicationShortCuts = Get-ShortCut $StringToSearch
forEach($ApplicationShortCut in $ApplicationShortCuts) {
$ApplicationName = $ApplicationShortCut.Name
$WorkingDirectory = $ApplicationShortCut.WorkingDirectory
$TargetPath = $ApplicationShortCut.TargetPath
$Arguments = $ApplicationShortCut.Arguments
if ((-not $WorkingDirectory) -and $TargetPath) {$WorkingDirectory = Split-Path -Path $TargetPath -Parent}
$ApplicationArray += [PSCustomObject]@{
Name = $ApplicationName
Version = $Null
InstallDate = $Null
Directory = $WorkingDirectory
Command = $TargetPath
Arguments = $Arguments
UninstallCommand = $Null
}
}
}
Return $ApplicationArray
}
Example :
Get-Application Edge
Name : MSEdgeRedirect
Version : 0.7.4.0
InstallDate : 20230830
Directory : C:\Program Files (x86)\Microsoft\Edge\Application
Command : C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe
Arguments :
UninstallCommand : C:\Program Files\MSEdgeRedirect\MSEdgeRedirect.exe /uninstall
Name : Microsoft Edge
Version :
InstallDate : 20230612
Directory : C:\Program Files (x86)\Microsoft\Edge\Application
Command : C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe
Arguments :
UninstallCommand : C:\Program Files (x86)\Microsoft\Edge\Application\Installer\setup.exe
--uninstall --msedge --channel=stable --system-level --verbose-logging
Name : Microsoft Edge Update
Version : 1.3.175.29
InstallDate :
Directory : C:\Program Files (x86)\Microsoft\Edge\Application
Command : C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe
Arguments :
UninstallCommand :