Windows PowerShell - Writing Windows Services in PowerShell RRS feed

  • General discussion

  • This article presents a novel and easy way to create Windows services, by writing them in the PowerShell scripting language rather than C# or C++. No more compilation, just a quick edit/test cycle that can be done on any system.

    Read this article in the May issue of MSDN Magazine

    Monday, May 2, 2016 9:03 PM

All replies

  • Interesting article. Is there any article that shows how Powershell can be used to call a REST based service?
    Wednesday, May 18, 2016 3:14 PM
  • I recently used this as the basis for a customer need, and really appreciated this. 

    I needed to make a slight modification to allow the service to run under a service account other than localsystem, in order to perform some network operations, something like (around line 773 in the latest version of code), plus a function anywhere that allows for flexibility in how many / variety / testing  of service accounts in your environment)

    $isSystem = IsSystemAccount -user $userName

    Function IsSystemAccount { Param ( [string] $user )

    $isSsytemResult = $false

    if ($user -match "myServiceAccountName") { $isSystemResult = $true }

    return $isSsytemResult


    You also need to set the service to run under the service account, and all the recovery options, but there's other powershell to do that.

    Saturday, May 28, 2016 10:23 PM
  • Please report PSService.ps1 bugs in its GitHub issues page:


    1) This is covered in the PSService.ps1 update I posted on GitHub a couple of days ago:


    2) I don't think this is a good idea. The service instance is started by the SCM, which does run as system. Changing this $isSystem variable definition will cause the user/system distinction logic to fail.

    3) Please detail (in a new github issue :-) ). I did test that a few months ago by putting it in a "\Program Files" subdirectory, and did not have any problem. Then I concluded that putting it in "\Windows\System32" was more standard.

    4) This _might_ be related to the service stop bugs, that forced me to publish an update on GitHub. Please retest with the latest version, and open a bug on GitHub if your problem still occurs.

    Friday, June 10, 2016 5:54 PM
  • So you wrote a sample service that is 1000 lines long. Thanks, but I'll skip.
    Friday, October 14, 2016 5:46 AM
  • Looks good, runs great, and yes, I run it under a different service account, with no bad side effects.
    Thursday, November 17, 2016 9:33 PM
  • I had made a windows service following this article. All works fine and I felt like I’m a really programmer :). But I have a problem - this service consumes too much memory. In fact, for about 6 hours service consumed 3 Gb memory and I think it’s not good. Maybe anyone faced with this problem and resolve it?
    Maybe someone could help me?
    Thanks in advance. 

    Monday, March 13, 2017 8:09 AM
  • I dont understand this comment, can you share the code so it works with a domain account and not just local system?
    Tuesday, September 5, 2017 11:13 AM
  • I know it's been a while since this article came out, but this is exactly what I'm looking for to setup a service. My only issue is that when CSC.exe compiles the c# code my antivirus (ESET) flags it as a trojan.

    If I remove the lines regarding importing advapi32.dll and any subsequent calls to setservicestatus then it doesn't get flagged, but then when the service is stopped or started outside of SCM, Services.msc doesn't reflect the actual state of the service.

    Tuesday, April 2, 2019 8:21 PM
  • You should report this to your antivirus provider as a false positive.

    They'll eventually update their virus signatures to avoid this false detection.

    Tuesday, October 1, 2019 10:37 AM
  • I agree with JFLarvoire - it's a false positive in your ESET - McAfee and Defender do not flag that behavior.  You can confirm independently with ESET vs the others on VirusTotal. 
    Tuesday, October 1, 2019 5:47 PM