Only return state of a service when using sc.exe query

You may try the following:

sc.exe query MyService | select-string state >> MyFileLocation\MyFile.txt

The result should be:

STATE              : 4  RUNNING

With "pure" powershell:

Get-Service MyService | select Status

Using sc.exe is the cmd way.

Since you are using PowerShell, it is advised to use the Powershell Function Get-Service.

This allows you to use the following code:

(Get-Service MyService).status

This results in:

Running

To print it directly to a file, you can use:

(Get-Service MyService).status | out-file "MyLocation\MyFile.txt" -append

And given that we use powershell, you could even do something like this:

$status = (Get-Service MyService).status
"The status of MyService is $status" | out-file -path "MyLocation\MyFile.txt" -append