Home > other >  Powershell list app which is not installed by match and notmatch
Powershell list app which is not installed by match and notmatch

Time:01-11

I would like to ask you how to list applications which are not installed from array with regex. Bellow is my code.

  1. I would like to list all instalation apps which match with $programy_zjisteni. Does it works
  2. I would like to list all not instalation apps which notmatch with $programy_zjisteni. Does not works.

Maybe I should use soma filter? Or?

Thank you for your help.

$nainstalovane_programy = (Get-WmiObject -Class Win32_Product).name

#Programy, ktere kontroluji
$programy_zjisteni = @(
        "landesk.*Agent"
        "Logmanager Event Sender"
        )

$installed_programs = @()
$not_installed_programs = @()

foreach ($program in $nainstalovane_programy)
{
    foreach ($x in $programy_zjisteni)
    {
        if ($program -match $x)
        {
            $installed_programs  = $program
        }
        else
        {
            $not_installed_programs  = $program 
        }
    }
}

$installed_programs
$not_installed_programs

CodePudding user response:

If I've understood you correctly, something like this ought to work:

$nainstalovane_programy = (Get-WmiObject -Class Win32_Product).name

#Programy, ktere kontroluji
$programy_zjisteni = @(
        "landesk.*Agent"
        "Logmanager Event Sender"
        )

$installed_programs = $nainstalovane_programy | Where-Object {
    $program = $_
    $is_installed = @($programy_zjisteni | Where-Object { $program -match $_ }).Length -gt 0
    $is_installed
}

$not_installed_programs = $nainstalovane_programy | Where-Object {
    $installed_programs -notcontains $_
}

$installed_programs
$not_installed_programs

If you actually want $not_installed_applications to be a list of the patterns that do not match any installed application, try with:

$not_installed_programs = $programy_zjisteni | Where-Object {
    $pattern = $_
    $is_not_installed = @($nainstalovane_programy | Where-Object { $_ -match $pattern }).Length -eq 0
    $is_not_installed
}
  •  Tags:  
  • Related