@@ -6,36 +6,48 @@ $AppToDetect = "Notepad++.Notepad++"
66
77Function Get-WingetCmd {
88
9- # Get WinGet Path (if admin context)
10- $ResolveWingetPath = Resolve-Path " $env: ProgramFiles \WindowsApps\Microsoft.DesktopAppInstaller_*_*__8wekyb3d8bbwe" | Sort-Object { [version ]($_.Path -replace ' ^[^\d]+_((\d+\.)*\d+)_.*' , ' $1' ) }
11- if ($ResolveWingetPath ) {
12- # If multiple version, pick last one
13- $WingetPath = $ResolveWingetPath [-1 ].Path
9+ $WingetCmd = $null
10+
11+ # Get WinGet Path
12+ try {
13+ # Get Admin Context Winget Location
14+ $WingetInfo = (Get-Item " $env: ProgramFiles \WindowsApps\Microsoft.DesktopAppInstaller_*_8wekyb3d8bbwe\winget.exe" ).VersionInfo | Sort-Object - Property FileVersionRaw
15+ # If multiple versions, pick most recent one
16+ $WingetCmd = $WingetInfo [-1 ].FileName
1417 }
15-
16- # Get Winget Location in User context
17- $WingetCmd = Get-Command winget.exe - ErrorAction SilentlyContinue
18- if ($WingetCmd ) {
19- $Script :Winget = $WingetCmd.Source
20- }
21- # Get Winget Location in System context
22- elseif (Test-Path " $WingetPath \winget.exe" ) {
23- $Script :Winget = " $WingetPath \winget.exe"
24- }
25- else {
26- break
18+ catch {
19+ # Get User context Winget Location
20+ if (Test-Path " $env: LocalAppData \Microsoft\WindowsApps\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe\winget.exe" ) {
21+ $WingetCmd = " $env: LocalAppData \Microsoft\WindowsApps\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe\winget.exe"
22+ }
2723 }
24+
25+ return $WingetCmd
2826}
2927
3028<# MAIN #>
3129
3230# Get WinGet Location Function
33- Get-WingetCmd
31+ $winget = Get-WingetCmd
32+
33+ # Set json export file
34+ $JsonFile = " .\InstalledApps.json"
35+
36+ # Get installed apps and version in json file
37+ & $Winget export - o $JsonFile -- accept- source- agreements | Out-Null
38+
39+ # Get json content
40+ $Json = Get-Content $JsonFile - Raw | ConvertFrom-Json
41+
42+ # Get apps and version in hashtable
43+ $Packages = $Json.Sources.Packages
44+
45+ # Remove json file
46+ Remove-Item $JsonFile - Force
3447
35- # Get "Winget List AppID"
36- $InstalledApp = & $winget list -- Id $AppToDetect -- accept - source - agreements | Out-String
48+ # Search for specific app and version
49+ $Apps = $Packages | Where-Object { $_ .PackageIdentifier -eq $AppToDetect }
3750
38- # Return if AppID existe in the list
39- if ($InstalledApp -match [regex ]::Escape($AppToDetect )) {
51+ if ($Apps ) {
4052 return " Installed!"
4153}
0 commit comments