I am attempting to write a powershell script to loop through an entire directory structure and provide the filename, path, and line number of any potentially sensitive information that is contained in the file. When I run the code below, it looks like all of the individual objects in the directory are merged into one called inputstream. Why is this happening?
Code:
Get-ChildItem -Recurse | Get-Content -ErrorAction SilentlyContinue | Select-String -Pattern "key.=" | Format-Table FullName, Path, LineNumber, Pattern;
Output:
FullName Path LineNumber Pattern
-------- ---- ---------- -------
InputStream 1653 key.=
InputStream 2550 key.=
InputStream 3405 key.=
InputStream 4302 key.=
InputStream 4584 key.=
Select-String
is reporting the object it was working on correctly. You piped file contents, via Get-Content
to the cmdlet. It was not a path to file so it does not know where that data came from. So it is telling you that InputStream was the source. This is corroborated via Microsoft:
The default output of Select-String is a MatchInfo object, which includes detailed information about the matches. The information in the object is useful when you are searching for text in files, because MatchInfo objects have properties such as Filename and Line. When the input is not from the file, the value of these parameters is InputStream.
emphasis mine
For what you are looking for you would need to change your logic slightly.
Get-ChildItem -Recurse | Select-String -Pattern "key.=" | Select Path, LineNumber, Pattern
Select-String
does accept file paths via the pipeline so using that you can get the results you are expecting. I also dropped Format-Table
as that can cause you heart ache later and FullName is not a property returned by Select-String
Warning that by default Select-String
supports regex and you have a regex control character in that string. Use -SimpleMatch
if you don't intend to use regex.