need help creating powershell to disable windows services

Discussion in 'Scripting' started by raptorddd, Jul 14, 2022.

Tags:
  1. raptorddd

    raptorddd MDL Addicted

    Aug 17, 2019
    617
    204
    30
    #1 raptorddd, Jul 14, 2022
    Last edited by a moderator: Jul 14, 2022
    i found this but not sure what to do.
    Code:
    #requires -RunAsAdministrator
    
    # comma seperated list of service names to set to manual
    $servicestodisable = "Fax","GamingServicesNet"
    
    # loop through each service and set to manual and stop the service.
    Foreach ($service in $servicestodisable) {
        $s = Get-service $service
        $s | Set-service -StartupType Manual
        $s | stop-service
    }
    
    say i want to disable smart card. just teach me how to do it and ill add the rest of services myself. i just need a way to disable selected services by me.
     
  2. case-sensitive

    case-sensitive MDL Expert

    Nov 7, 2013
    1,681
    731
    60
    #2 case-sensitive, Jul 14, 2022
    Last edited by a moderator: Jul 14, 2022
    Maybe start here ---- >
    Code:
    #   Description:
    # This script disables unwanted Windows services. If you do not want to disable
    # certain services comment out the corresponding lines below.
    ### Author of this script: https://github.com/W4RH4WK/Debloat-Windows-10
    
    $services = @(
        "diagnosticshub.standardcollector.service" # Microsoft (R) Diagnostics Hub Standard Collector Service
        "DiagTrack"                                # Diagnostics Tracking Service
        "dmwappushservice"                         # WAP Push Message Routing Service (see known issues)
        "lfsvc"                                    # Geolocation Service
        "MapsBroker"                               # Downloaded Maps Manager
        "NetTcpPortSharing"                        # Net.Tcp Port Sharing Service
        "RemoteAccess"                             # Routing and Remote Access
        "RemoteRegistry"                           # Remote Registry
        "SharedAccess"                             # Internet Connection Sharing (ICS)
        "TrkWks"                                   # Distributed Link Tracking Client
        "WbioSrvc"                                 # Windows Biometric Service (required for Fingerprint reader / facial detection)
        #"WlanSvc"                                 # WLAN AutoConfig (Disabling this can cause issues with wifi connectivity)
        "WMPNetworkSvc"                            # Windows Media Player Network Sharing Service
        #"wscsvc"                                  # Windows Security Center Service
        #"WSearch"                                 # Windows Search
        "XblAuthManager"                           # Xbox Live Auth Manager
        "XblGameSave"                              # Xbox Live Game Save Service
        "XboxNetApiSvc"                            # Xbox Live Networking Service
        "ndu"                                      # Windows Network Data Usage Monitor
        # Services which cannot be disabled
        #"WdNisSvc"
    )
    
    foreach ($service in $services) {
        Write-Output "Trying to disable $service"
        Get-Service -Name $service | Set-Service -StartupType Disabled
    }
    
     
  3. raptorddd

    raptorddd MDL Addicted

    Aug 17, 2019
    617
    204
    30
    #3 raptorddd, Jul 14, 2022
    Last edited by a moderator: Jul 14, 2022
    (OP)
    @case-sensitive

    so it would be like this.? disable windows search and smart card. so copy all the text below
    Code:
    $services = @(
    #"WSearch" # Windows Search
    # "SCardSvr" # Smart Card
    # Services which cannot be disabled
    #"WdNisSvc"
    )
    
    foreach ($service in $services) {
    Write-Output "Trying to disable $service"
    Get-Service -Name $service | Set-Service -StartupType Disabled
    }
    
    i just created a test.ps1 file but when i right click context have run with powershell it opens 1 second then closes.???
     
  4. case-sensitive

    case-sensitive MDL Expert

    Nov 7, 2013
    1,681
    731
    60
  5. case-sensitive

    case-sensitive MDL Expert

    Nov 7, 2013
    1,681
    731
    60
    I'm not saying that what you want to do makes any sense or that it will work .

    If i were you i'd look at the link where that script came from and look at the other things that are there .......... and what they do and how .

    It doesnt matter how long a script takes to do or not do something ........... its about if it works ? = Did it work ? If not why not ?

    I'm not playing games with you ......... its about you understanding what your trying to learn .
     
  6. raptorddd

    raptorddd MDL Addicted

    Aug 17, 2019
    617
    204
    30
    what am trying if a batch script that i can add ther services i want disabled..
    what i did on your script was just edit it with the services i want disabled. thats why am asking for help.. what i did is right or i made a mistake.?
    i tried both.. and both do nothing and both just closes in 1 second. i have 0 knowledge. if someone could edit the script for me if i did something wrong . then i can just add the rest correctly. i say correctly because am not sure if i made a mistake.
     
  7. case-sensitive

    case-sensitive MDL Expert

    Nov 7, 2013
    1,681
    731
    60
    I just looked in google to find out what # means in a powershell script ........... and couldnt find it :)

    # This script disables unwanted Windows services. If you do not want to disable
    # certain services comment out the corresponding lines below.

    >i tried both

    You mean the one i stole from the net and posted here ........ and your edited one ? The one i posted works for me as it is .
     
  8. Dark Dinosaur

    Dark Dinosaur X Æ A-12

    Feb 2, 2011
    3,760
    5,223
    120
    this will do the job.
    Code:
    Get-service | OGV -PassThru -Title 'Select Service To disable' | foreach {Set-service ($_).ServiceName -StartupType Manual; stop-service -Name ($_).ServiceName;}
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  9. Dark Dinosaur

    Dark Dinosaur X Æ A-12

    Feb 2, 2011
    3,760
    5,223
    120
    #10 Dark Dinosaur, Jul 15, 2022
    Last edited: Jul 15, 2022
    just ignore this post.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  10. case-sensitive

    case-sensitive MDL Expert

    Nov 7, 2013
    1,681
    731
    60
    #11 case-sensitive, Jul 15, 2022
    Last edited: Jul 16, 2022
    I'm out . The thread has been edited so that the original reason for it isnt clear any more . The thread was suposed to be about someone understanding and learning how to turn services off with a powershell script .
     
  11. raptorddd

    raptorddd MDL Addicted

    Aug 17, 2019
    617
    204
    30
    yes the one you posted. well actually ive tried both and both just closes powershell.

    did it worked for you. can you edit for windows search and let me run it.
     
  12. raptorddd

    raptorddd MDL Addicted

    Aug 17, 2019
    617
    204
    30
    will try this later on today
    thanks
     
  13. raptorddd

    raptorddd MDL Addicted

    Aug 17, 2019
    617
    204
    30
    can you please make the code line with service smart card SCardSvr so i know exactly where to replace with services i want to disable... cause i see 3 service name.
     
  14. raptorddd

    raptorddd MDL Addicted

    Aug 17, 2019
    617
    204
    30
    ok i just run ths one just like it is posted.. and it then opens a window to select services to disable... when i run selected services it shows red letters then closes.
     
  15. zbigniew59

    zbigniew59 MDL Senior Member

    May 14, 2016
    374
    171
    10
    #16 zbigniew59, Jul 23, 2022
    Last edited: Jul 23, 2022
    Read it - https://docs.microsoft.com/pl-pl/po....core/about/about_scripts?view=powershell-7.2
    Sample - Disable-services.ps1

    # Description:
    # This script disables unwanted Windows services. If you do not want to disable
    # certain services comment out the corresponding lines below.
    $services = @(
    "diagnosticshub.standardcollector.service" # Microsoft (R) Diagnostics Hub Standard Collector Service
    "DiagTrack" # Diagnostics Tracking Service
    "dmwappushservice" # WAP Push Message Routing Service (see known issues)
    "lfsvc" # Geolocation Service
    "MapsBroker" # Downloaded Maps Manager
    "NetTcpPortSharing" # Net.Tcp Port Sharing Service
    "RemoteAccess" # Routing and Remote Access
    "RemoteRegistry" # Remote Registry
    "SharedAccess" # Internet Connection Sharing (ICS)
    "TrkWks" # Distributed Link Tracking Client
    "WbioSrvc" # Windows Biometric Service (required for Fingerprint reader / facial detection)
    #"WlanSvc" # WLAN AutoConfig (Disabling this can cause issues with wifi connectivity)
    "WMPNetworkSvc" # Windows Media Player Network Sharing Service
    #"wscsvc" # Windows Security Center Service
    #"WSearch" # Windows Search
    "XblAuthManager" # Xbox Live Auth Manager
    "XblGameSave" # Xbox Live Game Save Service
    "XboxNetApiSvc" # Xbox Live Networking Service
    "ndu" # Windows Network Data Usage Monitor
    # Services which cannot be disabled
    #"WdNisSvc"
    )
    foreach ($service in $services) {
    Write-Output "Trying to disable $service"
    Get-Service -Name $service | Set-Service -StartupType Disabled
    }

    # - at the beginning, the service will not be disabled,
    If this - # - you remove it from the line - the service will be disabled
     
  16. raptorddd

    raptorddd MDL Addicted

    Aug 17, 2019
    617
    204
    30
    #17 raptorddd, Jul 23, 2022
    Last edited: Jul 26, 2022
    (OP)
    now i get it so # enable and no # services that starts without it will be disabled.
    thanks

    UPDATE
    yes this works... just tested. thanks