Powershell script to modify service startup type from Automatic to Manual

Hi

Can some help me to modify a powershell script to change STARTUP Type for some of services from AUTOMATIC to MANUAL ?

This script is for starting service. I need the same script to modify Startup Type.

$serverList  = gc servers.txt
$serviceList = gc services.txt

ForEach ($server in $serverList)
{
    ForEach ($service in $serviceList)
    {
        Get-Service -Name $service -ComputerName $server | Start-service
    }
}
michalek19Asked:
Who is Participating?
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
I've checked our answers again, and see we omitted the necessary -PassThru switch for Set-Service to allow the service object to be passed thru the pipeline for Start-Service.
My suggestion then wouldd be:
Get-Service -Name (gc services.txt) -ComputerName (gc servers.txt) | Set-Service -StartupType Manual -PassThru |  Start-Service

Open in new window

It didn't matter for my code, as Start-Service has been commented out, but of course it is not correct.
0
 
Jose Gabriel Ortega CEE Solution Guide - CEO Faru Bonon ITCommented:
it would be something like:

$serverList  = gc servers.txt
$serviceList = gc services.txt

ForEach ($server in $serverList){
    ForEach ($service in $serviceList){
     #this will set to manual and start the service, if you just want to change the automatic to manual, remove the | start-service
        Get-Service -Name $service -ComputerName $server | Set-Service -StartupType Manual |  Start-Service
    }
}

Open in new window


With elevated privilegies.

Note: this will set to manual and start the service, if you just want to change the automatic to manual, remove the | start-service in line 7
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Above answers your question directly, but it's much better to make use of the string array feature of Get-Service.
$serverList  = gc servers.txt
$serviceList = gc services.txt
Get-Service -Name $serviceList -ComputerName $serverList | Set-Service -StartupType Manual # |  Start-Service

Open in new window

or
Get-Service -Name (gc services.txt) -ComputerName (gc servers.txt) | Set-Service -StartupType Manual # |  Start-Service

Open in new window

0
 
yo_beeDirector of Information TechnologyCommented:
Qlemo would you be able to explain why its better to do it your method vs Jose.  
Yours look more straight forward, but is there any benefit in performance?
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
The code as shown by Jose has to query each computer for each single service one by one. So first optimization is to contact each PC once to get all services of interest, by providing the string array for services as a parameter. This improves performance.

Providing a computer array adds the option of parallel processing for some cmdlets. IIRC Get-Service does not run parallel threads, but it is still much more simple to just provide a string array of PCs.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.