I want to find the string value from the text file in powershell

chirag darji 136 Reputation points
2023-02-20T14:48:48.3333333+00:00

I get the installed program list and i want to match the each program to taxt file, in the taxt file i have already list of program name, i want to match each program list to taxt file


Clear
$Error.Clear()

$output = @()

$appname  = Get-Content -path $env:USERPROFILE\Downloads\Installed-Applications.txt

$INSTALLED = Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate
$INSTALLED += Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate
$INSTALLED += Get-ItemProperty HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate
$sysapp = $INSTALLED | ?{ $_.DisplayName -ne $null}

foreach($a in $sysapp)
{
    $b = $a.DisplayName
        

        if($b -in $appname)
        {

            $apps = [PSCustomObject]@{SystemApps=$a.DisplayName; ReuirApps=$applist}
        }
        else
        { 
             $apps = [PSCustomObject]@{SystemApps=$a.DisplayName; ReuirApps=$applist}
        }
       $output += $apps

    
  }
  return $output
 


Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 123.5K Reputation points MVP Volunteer Moderator
    2023-02-20T15:15:21.0366667+00:00

    Hi @chirag darji ,

    the variable $applistisn't set/defined in your script. For that reason the value of the variable is always empty.

    Please try this, after you fixed the $applistvariable:

    Clear-Host
    $Error.Clear()
    $apps = @()
    
    $appname = Get-Content -Path .\applist.txt
    
    $INSTALLED = Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate
    $INSTALLED += Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate
    $INSTALLED += Get-ItemProperty HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate
    $sysapp = $INSTALLED | Where-Object { $_.DisplayName -ne $null }
    
    foreach ($a in $sysapp) {
        $b = $a.DisplayName
            
    
        if ($b -in $appname) {
            $apps += [PSCustomObject]@{SystemApps = $a.DisplayName; ReuirApps = $applist }
        }
        else { 
            $apps += [PSCustomObject]@{SystemApps = $a.DisplayName; ReuirApps = $applist }
        }
        #$output += $apps
    }
    return $apps
    

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards

    Andreas Baumgarten

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.