Andrew Tucker Andrew Tucker - 2 months ago 6x
Powershell Question

Powershell Script To Display Stopped Processes With an input Parameter to filter

I'm pretty new to Powershell, but for the life of me I can't find how to do this anywhere on Google.

Here's what I need my script to look like when executing.

While I have managed to get my script to display the stopped processes. I haven't been able to get it to filter by parameter. ( I also have the parameter working as well)

Here's my Code.

param ([string]$myVariable)
"The following $myVariable Services are currently stopped on: $(HostName)"
Get-Service | Where-Object {($_.status -eq "stopped")
"List of stopped services complete."
"Script Terminating"
powershell -noexit

So far I've got it to display the stopped services. I've tried entering another parameter on the same line wit the get service, to force it to further filter the output. But Every time I do that, it always throws an error.

I've tried:
filtering by string
filtering by firstletter of (myVariable)

And I can't find anything else to try on Google. Everyone keeps recommending what I've already tried =
Any help would be greatly appreciated!! :)


You just need to change this one line.

Get-Service | Where-Object {($_.status -eq "stopped" -and $_.Name.Substring(0,1) -eq $myVariable)}

So it filters down the services to the ones that are stopped and then beginning with the letter passed from the paramater? I think that's what your asking for?