locked
How to search for string in an Array of objects, then store into a seperate Array if string is found. RRS feed

  • Question

  • I currently have an object of arrays that contains different files of where I would like to search for a string of anything beginning with 'ds'.  Here is what I have for the first array which is working correctly.

    $props = @{
        dir = $null
        id = $null
    }

    $myList = New-Object 'System.Collections.Generic.List[psobject]'


    $arr2 = Get-Childitem –Path $directory -Include *.properties -Exclude *.ext -Recurse -ErrorAction SilentlyContinue
         
                  foreach ($dir in $arr2) {
                  
                  $myCustomObject = New-Object psobject -Property $props

                  $myCustomObject.dir = $dir
                                              
                  $myList.Add($myCustomObject.dir)
               
             }

    Write-Host $myList

    The problem I am having is I am needing to search through all the objects in $myList and store only the search string's that are found beginning with 'ds' into a seperate array to use later in my script.  Everything I have found so far only returns the files that contain the search string I am searching for.  Should I be using Get-Content instead of search string for this purpose.  I am very new to PowerShell scripting so any help is greatly appreciated.  Thanks!

    Wednesday, February 14, 2018 5:45 PM

Answers

  • Select-String can do that. You can specify a file specification to search, or you can use Get-ChildItem and pipe the FileInfo objects to Select-String and search that way.

    That's the best general advice I can give right now. If you need help with specific code, please write a short sample script that contains only the absolute minimum amount of code needed to reproduce the problem and post it here. Also explain specifically what isn't working and, most importantly, how it isn't working. (Remember, we don't have your data and we can't see your screen.)


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by Bill_Stewart Monday, March 19, 2018 5:39 PM
    Wednesday, February 14, 2018 6:23 PM

All replies

  • The question is a bit hard to understand. I'm not sure what you are trying to do, but PowerShell already has the Select-String cmdlet that can find strings in files.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, February 14, 2018 5:47 PM
  • Basically, I am wanting to search through a list of files and return all matches found to store into an array (or list).  There may be only one match of the search string or multiple matches depending on which computer the script is ran on.  The main problem I am having is searching though each file and then returning only the search string 'ds*' if found.
    Wednesday, February 14, 2018 6:04 PM
  • Select-String can do that. You can specify a file specification to search, or you can use Get-ChildItem and pipe the FileInfo objects to Select-String and search that way.

    That's the best general advice I can give right now. If you need help with specific code, please write a short sample script that contains only the absolute minimum amount of code needed to reproduce the problem and post it here. Also explain specifically what isn't working and, most importantly, how it isn't working. (Remember, we don't have your data and we can't see your screen.)


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by Bill_Stewart Monday, March 19, 2018 5:39 PM
    Wednesday, February 14, 2018 6:23 PM