0

So i am currently tackling a task I have been given as a challenge from my teacher, but it is safe to say I am pretty stumped. I am supposed to find a log file on a virtual machine drive which contains the specific keyword "x".

Is there a specific command I could use to search through every file for that keyword?

Thanks in advance :D

0

3 Answers 3

1

Select-String is the cmdlet you're looking for.

dir *.log | select-string 'stringtofind' -simplematch

or

dir *.log | select-string 'regex pattern

will output matching lines. The output is objects which points to the file, the line number, and the matching text (among other things).

Sign up to request clarification or add additional context in comments.

2 Comments

is there a way to automate searching through every directory?
Sure... use the -Recurse switch on dir (Get-ChildItem)
0

There is no command I am aware of that will directly search for a file with a specific content. You will have to open each file, and use regex to find the string. You would use Get-Childitem to get a list of each file in the log directory, and then foreach item you would Get-Content and then check if that content has the string you are looking for.

However, if you wanted an A+, you could run the task of searching each file as a job or a runspace to check each file in parallel/new thread, which would greatly increase the speed of sorting through many files, but that is a more advanced technique.

Part of the lesson is learning how to code this, so I am omitting the code, and giving you the key commands you should be looking at.

Comments

0

you might be able to do something like:

Get-Childitem -Path path\to\search -Recurse -ErrorAction SilentlyContinue | Select-String -pattern "jonathan.williams" | group path | select name

Update:

You can change it to

Get-Childitem -Path path\to\search -Recurse -ErrorAction SilentlyContinue | Select-String "jonathan.williams"

to get the line number along with the file as well

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.