Solved

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

Posted on 2016-11-21
4
40 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
  • 2
  • 2
4 Comments
 
LVL 83

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 83

Accepted Solution

by:
oBdA earned 500 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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 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