Searching file string improvement code

Dear expert

Now I beening looking for a way to improve my code below, now I want to search a string "test" inside a folder c: \logs\ now below script works fine, now I want to improve this by everytime running the script, it will ask me for folder name and string name. Anyone know how to do this?

Get-ChildItem “c:\logs” -recurse | Select-String -pattern "test” | group path | select name

Thanks
LVL 1
WeTiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

WeTiAuthor Commented:
Solved:
$Folder = Read-Host -Prompt 'Where you want to search folder'
$Text = Read-Host -Prompt 'String for the search'
Get-ChildItem $Folder -recurse | Select-String -pattern $Text | group path | select name
WeTiAuthor Commented:
If anyone got better idea, please type here below, I will see if anyone got better solutions then I will assign points to him/her. Thanks.
oBdACommented:
It's more versatile if you define them as arguments; you can either pass them directly using
.\Whatever.ps1 -Path C:\Logs -Pattern test
or you'll be queried if you omit one or both of the arguments
.\Whatever.ps1 -Pattern test
Param(
	[Parameter(Position=0, Mandatory=$True, HelpMessage='The path in which to start the search')][ValidateNotNullOrEmpty()]
	[String]$Path,
	[Parameter(Position=1, Mandatory=$True, HelpMessage='The pattern to start search for')][ValidateNotNullOrEmpty()]
	[String]$Pattern
)
Get-ChildItem -Path $Path -Recurse -File | Select-String -Pattern $Pattern | Group-Object Path | Select-Object Name

Open in new window

SD-WAN: Making It Work for You

As bandwidth requirements and Internet costs grow, businesses naturally want to manage budgets by reducing reliance on their most expensive connection types. Learn more about how to make SD-WAN work for your business in our on-demand webinar!

WeTiAuthor Commented:
Hi, thanks, I now want to add more info showing, now this script only shows file name, I want to show Detail like when the file is created and which line the string is located, is this possible?
oBdACommented:
Param(
	[Parameter(Position=0, Mandatory=$True, HelpMessage='The path in which to start the search')][ValidateNotNullOrEmpty()]
	[String]$Path,
	[Parameter(Position=1, Mandatory=$True, HelpMessage='The pattern to start search for')][ValidateNotNullOrEmpty()]
	[String]$Pattern
)
Get-ChildItem -Path $Path -Recurse -File |
	Select-String -Pattern $Pattern |
	Group-Object Path |
	ForEach-Object {
		$LWT = (Get-Item -Path $_.Name).LastWriteTime
		$_.Group | Select-Object Path, LineNumber, @{n='LastWriteTime'; e={$LWT}}
	}

Open in new window

WeTiAuthor Commented:
Great script, thanks but now I saw the because the file path is too long it became like:
c:\windows\users\downloads\logs\1345...
The path is not showing complete row, the rest is "..."
oBdACommented:
That's just a display issue, the object retunred will have the full name. In table format, the column size is limited by definition.
Assign the output to a variable and inspect the variable, or pipe it to Format-Table -AutoSize, or, if the path is still too long, pipe it to Format-List
WeTiAuthor Commented:
Please give me the whole script with AutoSize. And also Format-List. Sorry if i ask too much, I tried add it, but it doesn't work.
oBdACommented:
In general, scripts shouldn't return output generated by "Format-*" cmdlets, because it can't be used any further. The "Format-*" cmdlets should be applied to the script's output on an as-needed basis.
In other words, use
.\Whatever.ps1 -Path C:\Logs -Pattern 'test' | Format-Table -AutoSize

Open in new window

.\Whatever.ps1 -Path C:\Logs -Pattern 'test' | Format-List

Open in new window

WeTiAuthor Commented:
This means the script you created before in the Param {... not working with | Format-Table -AutoSize?
oBdACommented:
Of course it will. But the script currently returns proper objects that can be processed further. Adding a Format-Whatever as last pipeline stage inside the script will turn the objects into a bunch of eye candy that can't be processed any further.
Something like
.\Whatever.ps1 -Path C:\Logs -Pattern 'test' | Export-Csv -NoTypeInformation -Path C:\Temp\SearchResults.csv

Open in new window

or
$Results = .\Whatever.ps1 -Path C:\Logs -Pattern 'test'
$Results | Format-Table -AutoSize
$Results | Format-List

Open in new window

would be totally impossible if the script would already generate Format-Whatever output.
WeTiAuthor Commented:
I don't understand. this Whatever.ps1 what is this? And what does -Path C:\Logs -Pattern 'test' do? I would like to type in_data in the script, this way you basicly command where is going to search and the path, but I want to type in data myself.
oBdACommented:
Whatever.ps1 would be the script from https:#a42458991
Because you can save it as Whatever.ps1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WeTiAuthor Commented:
$Results = .\Whatever.ps1 -Path C:\Logs -Pattern 'test'
$Results | Format-Table -AutoSize
$Results | Format-List

I understand that you create a $Results but why you do need -Path C:\Logs -Pattern 'test'? In whatever.ps1 you already point the path and string?
oBdACommented:
In whatever.ps1 you already point the path and string?
No I don't.
See https:#a42458344
WeTiAuthor Commented:
Well you get the point now. Whatever we do seems this function is very very small in the powershell and it well does not work 100%
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.