?
Solved

How can I force the file content search to include all subdirectories?

Posted on 2016-11-21
4
Medium Priority
?
44 Views
Last Modified: 2016-11-21
Hi Expert,

I manage to lookup and display the full line of content of all txt file (based on the criteria below).

However, I would like:

1) the searches to include all subdirectories (not just from the root)
2) output both the the respective subfoldername * filename of detected file which contents the target keyword (other than just the text line)

My current ps1 script:-

$Content = Get-Content E:\*.txt
$Content | Foreach {
                    if ($_ -like "CurrentEdition=*,*") {Write-Output $_}
                    }


Please help.
0
Comment
Question by:Brian Smith
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 85

Expert Comment

by:oBdA
ID: 41895772
Try this:
Get-ChildItem E:\*.txt -Recurse | ForEach-Object {If (Select-String -Path $_.FullName -Pattern 'CurrentEdition=.*,.*') {$_}} | Select-Object DirectoryName,
Name

Open in new window

1
 

Author Comment

by:Brian Smith
ID: 41896009
Hi oBdA,

Thank you for your suggestion.

Your ps1 commandline given did list down all the *.txt filename + the respective directory (including sub-directories).

So now, how can I combined both of my script and your's together, so that the end result will show me both (the entire line of file content that met the search criteria together with the respective filename + the path location) info simultaneously?
0
 
LVL 85

Accepted Solution

by:
oBdA earned 2000 total points
ID: 41896084
Get-ChildItem "E:\*.txt" -Recurse | ForEach-Object {
	$FileItem = $_
	Select-String -Path $FileItem.FullName -Pattern 'CurrentEdition=.*,.*' |
		Select-Object -Property `
			@{Name='DirectoryName'; Expression={$FileItem.DirectoryName}},
			@{Name='Name'; Expression={$FileItem.Name}},
			LineNumber,
			Line
}

Open in new window

1
 

Author Closing Comment

by:Brian Smith
ID: 41896105
Perfect! Thank you so much for making my day  :)
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A procedure for exporting installed hotfix details of remote computers using powershell
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question