Skip to content
This repository was archived by the owner on Dec 5, 2025. It is now read-only.

Commit 1979ea0

Browse files
authored
Merge pull request #92 from Romanitho/dev
WAU improvements implementation
2 parents 29d5be0 + 77297dc commit 1979ea0

File tree

2 files changed

+298
-166
lines changed

2 files changed

+298
-166
lines changed

winget-detect.ps1

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,48 @@ $AppToDetect = "Notepad++.Notepad++"
66

77
Function 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

Comments
 (0)