Help filtering a list of software titles using regex

This topic contains 6 replies, has 3 voices, and was last updated by  Joe Fecht 3 months, 1 week ago.

  • Author
    Posts
  • #94044

    Joe Fecht
    Participant

    Hello – I'm trying to pull a list of software installed on multiple computers. I've part of my script block below.

    Everything is working as I would expect it except for the "if ($SWDisplayName -notmatch $UnwantedSW){...."

    No matter what I have tried to get the -Match or -NotMatch operator to work it won't place nice.

    If anyone has any input please let me know.

    # Place holder for when this runs in the scriptblock for invoke command
    $Computer= "Bob"
    
    #List of SW titles that I'm looking to filter out
    $UnwantedSWTitles= "Microsoft office","sql","exchange",".NET","C++","VMware","7-zip","Report Viewer","Sharepoint","Citrix","IIS","Silverlight","NSClient","Visual Studio","Adobe","Java","Google","Firefox","PDF","Apache"
    
    #Converts the UnwantedSWTitles string to a regex variable 
    [regex]$UnwantedSW = '^(' + (($UnwantedSWTitles|foreach {[regex]::escape($_)}) –join '|') + ')$'
    
    #Gets a list of all x86 based software installed on the system
    $SWx86 = Get-ChildItem "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" | Get-ItemProperty
    
    #Filters through each software title
    Foreach ($SW in $SWx86){
    
        #Grabs the SW name (I know it's the old way of doing it but making sure it v2 compatible)
        $SWDisplayName = $SW | Select-Object -ExpandProperty displayname -ErrorAction SilentlyContinue
        
        #Checks to see if the software's display name  is listed in the UnwantedSW list
        #This is what I'm having trouble filtering on
        if ($SWDisplayName -notmatch $UnwantedSW){
        
            #Pulls the info from the SW title if its display name does NOT contain any words from within the $UnwantedSWTitles 
            $Prop = @{ComputerName = $Computer;
                             SWDisplayName = $SW| Select-Object -ExpandProperty Displayname -ErrorAction SilentlyContinue;
                             SWVendor = $SW | Select-Object -ExpandProperty Publisher -ErrorAction SilentlyContinue;
                             SWVersion = $SW | Select-Object -ExpandProperty DisplayVersion -ErrorAction SilentlyContinue;
                             SWInstallDate = $SW| Select-Object -ExpandProperty InstallDate -ErrorAction SilentlyContinue}
    
            Write-Output $Prop
                        
        }
    }
    
  • #94045

    Christian
    Participant

    Why are you using a regex and not an array for your unwanted software titles?

  • #94047

    Joe Fecht
    Participant

    Speed was the main reason but if I can make it work without Regex then I'm open to anything at this point.

    • #94050

      Christian
      Participant

      I think there is a typo in line 14.

      You are looping through $SWx64 but this is not defined in your script. In line 11 $SWx86 is defined.

  • #94053

    Joe Fecht
    Participant

    Forgot to say thank you Christian for looking this over.

    Yes, that was a typo on my end. I tested after fixing that and it still isn't working. I've updated the code block above as well.

  • #94056

    iain Barnetson
    Participant

    I'm a Regex beginner, but when I ran the process encapsulating the software names with \b....\b it worked for me.

    
    $UnwantedSWTitles = "\bMicrosoft office\b|\bsql\b|exchange\b|\b.NET\b|\bC\+\+\b|\bVMware\b|\b7-zip\b|\bReport Viewer\b|\bSharepoint\b|\bCitrix\b|\bIIS\b|\bSilverlight\b|\bNSClient\b|\bVisual Studio\b|\bAdobe\b|\bJava\b|\bGoogle\b|\bFirefox\b|\bPDF\b|\bApache\b"
    
    
    • #94060

      Joe Fecht
      Participant

      This worked really well. I appreciate your help with this. Had a few small tweaks but overall this is great. Thank you!!

You must be logged in to reply to this topic.