Search code examples
powershellsplitselect-string

PowerShell Split Select String


I have the following command

Get-ChildItem -Recurse -Path 'C:\root' | Select-String -Pattern '172\.20\.' | group | select name | export-csv -Path 'C:\test.csv'

This command goes through .zone files and returns the path of the file and the string in which my pattern is matched.

The problem is, is that result looks like:

name
C:\root\test.com.zone:22:test.com.900 IN TXT "v=spf1 mx ptr ip4:172.20.1.1 -all"

how can i split the result so that I have my filename first and the value in a different column? This is needed since i can't split them in a .csv file


Solution

  • you dont need to group :

    Get-ChildItem -Recurse -Path 'C:\root' | 
     Select-String -Pattern '172\.20\.' | 
       select Path,Linenumber,Line | 
         export-csv -Path 'C:\test.csv'
    

    With Grouping