Filter rows in an Excel Document based on specific word in a sentence and extensions Using PowerShell

Tphelps 1 Reputation point
2020-10-13T14:49:39.83+00:00

31970-capture.png

In the attached excel spreadsheet I would like to filter the excel if word 'Monday' or 'repair' or 'slides' is in Name column . Also filter if any file have extension pdf (file also contains the words like 1st or Monday or budget etc).
I am really a newbie in Power Shell, i have got below so far which i am barely starting , can you please advise on how should i do it.

Import-Csv -Path "C:\Temp\Results.csv"  | ? Name -like *Monday* or *Staff*| Export-Csv test.csv  

Thanks

Excel Management
Excel Management
Excel: A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.Management: The act or process of organizing, handling, directing or controlling something.
1,706 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,507 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 46,551 Reputation points
    2020-10-13T18:47:34.083+00:00

    This is somewhat crude, but it matches your problem description. If you meant that a "word" can only be considered to be a word if it was surrounded by regular expression word break characters (i.e. "\b") or just non-word characters (or any characters of your choice, really), some of the crudeness would disappear.

    $wordlist1 = "monday","repair","slides"     # NOT used with PDF files
    $wordlist2 = "1st","monday","budget"        # used with PDF files
    
    import-csv c:\junk\f.csv |
        ForEach-Object{
            $emit = $false
            $line = $_
            if($_.Extension -eq '.pdf'){
                $wordlist2 |
                    ForEach-Object{
                        if ($line.Name -match "$_"){
                            $emit = $true
                        }
                    }
                if ($emit){
                    $line
                }
            }
            else{
                $wordlist1 |
                    ForEach-Object{
                        if ($line.name -match $_){
                            $emit = $true
                        }
                    }
                if ($emit){
                    $line
                }
            }
        } | Export-Csv c:\junk\f1.csv
    
    1 person found this answer helpful.
    0 comments No comments

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.