PS script to check services via get-content not running

I have the short  script here that  prompt error when I run them.

 Get-Content "MYserverList.txt" | % {
Write-Host "Checking $($_) .."
Get-Service -ComputerName | ?{$_.Displayname -match "SQL*"} |%{$_.stop()}

However the s script below running fine without error.

Get-Service -ComputerName servername1,servername2,servername3 | ?{$_.Displayname -match "SQL*"}|%{$_.stop()}
Who is Participating?
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.

Main issue: you forgot to specify the loop variable $_ for the ComputerName argument.
Other issues: -match expects a regular expression; the RegEx "SQL*" matches anything starting with SQ, followed by any amount (including zero) of "L"; so "SQUARE" would match as well. Use -like for simple wildcards.
Or even better, don't use the Where-Object at all, since Get-Service support wildcards in the DisplayName. Easier on the resources, because the remote machines will only send back the matching services, instead of each and every one, most of which your script will then throw away.
Get-Content "MYserverList.txt" | ForEach-Object {
	Write-Host "Checking $($_) .."
	Get-Service -Displayname "SQL*" -ComputerName $_ | ForEach-Object {$_.Stop()}

Open in new window

And you shouldn't use aliases (%, ?, dir, ...) in scripts. They're OK in an interactive command line, but in scripts, the full cmdlet name should be used - you never know if the script will run on a machine where an alias was redefined.

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
Solution provided.
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

From novice to tech pro — start learning today.