PowerShell Restart-Service | Start-Service issue: Why can't Windows Service be longer than 80 characters?

Diederik 21 Reputation points
2022-09-07T11:18:29.557+00:00

Hi,

I noticed that when having installed a Windows service, PowerShell Start-Service or Restart-Service can't handle services with a name longer than 80 characters. Not clear why though.

How to reproduce this (BinaryPath is notepad.exe but that is actually irrelevant to show the error): run in PowerShell (admin)

PS C:\Users\admin> New-Service -Name "TestingLengthOfServiceNameWith--------------------------------------80characters" -BinaryPathName %windir%\system32\notepad.exe  
  
PS C:\Users\admin> New-Service -Name "TestingLengthOfServiceNameWith---------------------------------------81characters" -BinaryPathName %windir%\system32\notepad.exe  

Now I get (when starting):

PS C:\Users\admin> Get-Service -name *80Characters | Start-Service  
Start-Service : Service 'TestingLengthOfServiceNameWith--------------------------------------80characters (TestingLengthOfServiceNameWith--------------------------------------80characters)' cannot be started due to the following error: Cannot start service TestingLengthOfServiceNameWith--------------------------------------80characters on computer '.'.  
At line:1 char:35  
+ Get-Service -name *80Characters | Start-Service  
+ ~~~~~~~~~~~~~  
    + CategoryInfo : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Start-Service], ServiceCommandException  
    + FullyQualifiedErrorId : CouldNotStartService,Microsoft.PowerShell.Commands.StartServiceCommand  
  
PS C:\Users\admin> Get-Service -name *81Characters | Start-Service  
Start-Service : Service name TestingLengthOfServiceNameWith---------------------------------------81characters contains invalid characters, is empty, or is too long (max length = 80).  
At line:1 char:35  
+ Get-Service -name *81Characters | Start-Service  
+ ~~~~~~~~~~~~~  
    + CategoryInfo : NotSpecified: (:) [Start-Service], ArgumentException  
    + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.StartServiceCommand  

The error for the 81 characters is the one I am interested in. Why am I allowed to create services with long names (apparently it should be possible up to 256 characters according to this), if somewhere down the line a smaller value is checked to determine continuing with starting services?

Couldn't find a confirmed bug about this anywhere or documentation in PowerShell that those commands don't work with "too long" service names. So why is this happening? Where should I report a bug about this?

P.S. the script colouring did now show me that with the start commands error feedback, the 81 string wasn't put between single quotes. Might that help to pinpoint what is going wrong?

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,462 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 32,911 Reputation points
    2022-09-07T16:11:21.617+00:00

    It's probably limited to 80 characters so that it fits on an IBM punched card.

    Sorry, I digress. I was able to recreate the problem with PS 5.1 on Win10. It could be fixed in PS 7, I don't have that installed anywhere to test with. I see that Remove-Service was added in PS 6.0, so maybe other changes were made.

    I'll speculate that the developers might have wondered "why would anyone define a service with with a really long name when the DisplayName would seem to be the better place for a verbose value?".

    Looks like net.exe can start the service or you can use a shorter name and use DisplayName for the longer text.

    $Name80 = "TestingLengthOfServiceNameWith--------------------------------------80characters"   
    $Name81 = "TestingLengthOfServiceNameWith---------------------------------------81characters"   
      
    $Name80.Length  
    $Name81.Length  
      
    New-Service -Name $Name80 -BinaryPathName %windir%\system32\svchost.exe  
    New-Service -Name $Name81 -BinaryPathName %windir%\system32\svchost.exe  
      
    "Start 80"  
    Get-Service -Name $Name80  | Start-Service  
    ""  
    "Start 81"  
    Get-Service -Name $Name81  | Start-Service  
    ""  
    "Net 80"  
    net.exe start $Name80  
    "Net 81"  
    net.exe start $Name81  
    ""  
    "Try generic"  
    Get-Service -Name "testing*"  | Start-Service  
    ""  
    "Can we query them? "  
    sc.exe qc $Name80   
    sc.exe qc $Name81   
    ""  
    "Now delete them"  
    sc.exe delete $Name80   
    sc.exe delete $Name81   
    

    Or use display name.

    $Name80 = "Testing80"   
    $Name81 = "Testing81"   
      
    $DName80 = "TestingLengthOfServiceNameWith--------------------------------------80characters"   
    $DName81 = "TestingLengthOfServiceNameWith---------------------------------------81characters"   
      
    New-Service -Name $Name80 -DisplayName $DName80 -BinaryPathName %windir%\system32\svchost.exe  
    New-Service -Name $Name81 -DisplayName $DName81 -BinaryPathName %windir%\system32\svchost.exe  
      
      
    "Start 80"  
    Get-Service -Name $Name80  | Start-Service  
    Get-Service -DisplayName $DName80  | Start-Service  
    ""  
    "Start 81"  
    Get-Service -Name $Name81  | Start-Service  
    Get-Service -DisplayName $DName81  | Start-Service  
    ""  
    "Net 80"  
    net.exe start $Name80  
    "Net 81"  
    net.exe start $Name81  
    ""  
    "Try generic"  
    Get-Service -Name "testing*"  | Start-Service  
    ""  
    "Can we query them? "  
    sc.exe qc $Name80   
    sc.exe qc $Name81   
    ""  
    "Now delete them"  
    sc.exe delete $Name80   
    sc.exe delete $Name81   
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Diederik 21 Reputation points
    2022-09-08T09:23:57.42+00:00

    Thanks @MotoX80 !

    Seems then like an oversight to have added backwards compatibility support for 80 characters in one place but not verify that on creation of the service.

    Regardless, I read your comment that they fixed it in PS 7.2.6 so will try that out.

    Having used PS 5 by default, I checked the LTS and don't see PS5 mentioned there, so guessing that is indeed not receiving fixes. I'm a bit confused though that no longer supported, PS5 does not get upgraded to PS7 and you have to install it manually/separately... Seems a bit odd to have that as the default PS if it is no longer supported. Anyway, the fact that it should be fixed now let me to conclude that this was in fact undesired behaviour.

    0 comments No comments