I assume you’re using different versions of PowerShell, don’t you?
You may compare the version 7.2
with the version 5.1
Newer version do not have the parameter -ComputerName anymore. You will have to use explicit remoting with Invoke-Command instead.
Regardless of that … when you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.
I think you had the right idea to begin with just need to sort out parameters.
Recommendation, update the local help files and review examples in whatever version you are using. The included examples are usually decent.
Update-Help
Get-Help Get-Service -Examples
Get-Help Invoke-Command -Parameter ScriptBlock #or any other parameter name
Get-Help Invoke-Command -Examples
Great to hear that. BUT!!! Please format your code as code. You can read in my first answer how to do that.
First of all … you should filter INSIDE the script block.
Then …
If you want to limit the output to services where the name starts with an “M” you could use the cmdlet by itself:
Get-Service -Name M*
or you use a filter with Where-Object where you could apply even more complex filter rules if needed. In this simple case this would enough:
Get-Service |
Where-Object {$_.Name -like 'M*'}
Of course it does not. What output did you expect? You saved the only possible output in a variable buit you did not output this variable.