Not able to search string in PS1 files

This topic contains 1 reply, has 2 voices, and was last updated by  Olaf Soyk 10 months, 2 weeks ago.

  • Author
  • #74530

    Venkata Kalyan

    Wrote a script which will search through *.bat & *.ps1 file and provide the output.

    $Results = Foreach ($Server in (get-content "C:\Users\temp\Desktop\Test\Search_String\Server.txt"))
    {get-childitem \\$Server\c$\test\ -recurse -Include *.bat, *.ps1 | SELECT-STRING -pattern '$4ret&yrt5' -SimpleMatch | Select @{Name="Server";Expression={$Server}},path,Linenumber
    $Results | Select Server,path,Linenumber | Export-Csv C:\Users\temp\Desktop\Test\Search_String\string_match_results.csv -NoType

    I am getting output which has string in ".bat" files. But not able to get output for ".PS1" files.
    Can someone help me, where I am doing wrong.


  • #74534

    Olaf Soyk

    Did you confirm that there are *.ps1 files in the given location? Did you confirm that there are some occurences of the searched pattern in the files? Did you confirm that your Get-ChildItem delivers the output you expect?
    For the future: Please format your code as code and consider to indent your code and try not to use aliasses or abbreviations. This way would it be much easier to read and understand and of course to debug the code.

    $ServerList = get-content 'C:\Users\temp\Desktop\Test\Search_String\Server.txt'
    $Results = Foreach ($Server in $ServerList){
        Get-ChildItem '\\$Server\c$\test\*' -Recurse -Include *.bat, *.ps1 | 
            Select-String -Pattern '$4ret&yrt5' -SimpleMatch | 
                Select-Object @{Name='Server';Expression={$Server}},Path,Linenumber
    $Results | 
        Select Server,path,Linenumber | 
            Export-Csv C:\Users\temp\Desktop\Test\Search_String\string_match_results.csv -NoTypeInformation

You must be logged in to reply to this topic.