How to filter info more efficiently using Where-Object -filterscript

I am using a script that gets installed software and I want it to get Visio and Project installs.

Here is what I have that works well, but is there another method I can use, perhaps regex, or something that doesn’t require so many lines of code?

# Step 1: Declare path variables

$Path = "\MyDFSRoot\Share\Project_Visio_Installs"

$FileName = "$env:COMPUTERNAME-Project_Visio_Installs.log"


# Step 2: Create the filter to retrieve the desired installed applications

Get-InstalledApp | 
        Where-Object -FilterScript { ($_.AppID -match "{") -and ($_.AppName -match "Project") -or ($_.AppName -match "Visio") -and ($_.AppName -notmatch "MUI") -and ($_.AppName -notmatch "Service Pack") -and ($_.AppName -notmatch "Update") -and ($_.AppName -notmatch "Viewer") } |
            Select-Object -Property * -OutVariable OutPut | 
                Out-Null
                
                
# Step 3: Create the directory if the OutPut variable is not null

If ($OutPut -ne $null)
{
    If (!(Test-Path -Path $Path))
    {
        New-Item -Path $Path -ItemType Directory | Out-Null
    }

    If (Test-Path -Path "$Path\$FileName")
    {
        Exit
    }
}


# Step 4: Create a variable to use to help limit output duplications that were not caught in the first filter in Step 3 

$OutPut1 = @(
    If ($OutPut -eq $null)
    {
        Exit
    })

$OutPut2 = @(
    If ($OutPut.Count -eq '1')
    {
        $OutPut
    })
    
$OutPut3 = @(
    If ($OutPut.Count -ge '2')
    {
        $CheckProjDup = $OutPut |  
            Where-Object -FilterScript { $_.AppName -match "Project" }

        $CheckVisDup = $OutPut | 
            Where-Object -FilterScript { $_.AppName -match "Visio" }

        If ($CheckProjDup.Count -gt '1')
        {
            $CheckProjDup | 
                Where-Object -FilterScript { ($_.AppID -match "{") }

            $CheckVisDup
        }

        ElseIf ($CheckVisDup.Count -gt '1')
        {
            $CheckProjDup
            
            $CheckVisDup | 
                Where-Object -FilterScript { ($_.AppID -match "{") } 
        }
    })


#Step 5: Create the logic to evaluate if the check duplicates variable has a null result, if so, exit, else output the results to the log file

$OutPut1

$EmailRecipients = "AssetManagementDL@mydomain.com"


If ($OutPut2) 
{
    $OutPut2 | Out-File -FilePath "$Path\$FileName"
    
    $MailBody = $OutPut2 | Out-String
    
    Send-MailMessage -To $EmailRecipients -From "Test Admin" -Subject "Microsoft Project and Visio Installs Alert" -Body $MailBody -SmtpServer 'test.domain.com'
}

If ($OutPut3)
{
    $OutPut3 | Out-File -FilePath "$Path\$FileName"
    
    $MailBody = $OutPut3 | Out-String
    
    Send-MailMessage -To $EmailRecipients -From "ITDepartment@mydomain.com" -Subject "Microsoft Project and Visio Installs Alert" -Body $MailBody -SmtpServer 'smtp.mydomain.com'
}{content}

Yep, regex has an “or” operator build-in, so you could combine some of your expressions like this:

($_.AppID -match "{") -and ($_.AppName -match "Project|Visio") -and ($_.AppName -notmatch "MUI|Service Pack|Update|Viewer"))

Oh that’s cool. I will use that and see how it works for me and get back to you.

Thanks Dave

That did it for me, Dave.

Thanks again for your help, much appreciated.