Skip to content

Restart-Service should call systemctl restart on Linux OSes with SystemD present. #18863

Open

Description

Summary of the new feature / enhancement

I want to be able to restart sshd from Windows and Linux in the same way after modifying sshd_config. This is almost possible, because Get-Content and Set-Content are already cross-platform. However, I currently must interact with the init-system when restarting services on Linux, despite an abstraction for net and sc (Restart-Service) already existing in PowerShell, and the potential solution being, I expect, so easy.

Attempt

  1. #!/usr/bin/env pwsh
    Restart-Service 'sshd'

    1

  2. However, this produced:

    Restart-Service: The term 'Restart-Service' is not recognized as a name of a cmdlet, function, script file, or executable program.
    Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Proposed technical implementation details (optional)

Call systemctl restart sshd, if Get-Command systemctl returns True:

  1. #!/usr/bin/env pwsh
    systemctl start 'sshd' && `
    journalctl -fexu 'sshd'

    2

  2. Dec 28 18:29:10 beedell.rokejulianlockhart.1669806072 systemd[1]: Starting sshd.service - OpenSSH server daemon...
    ░░ Subject: A start job for unit sshd.service has begun execution
    ░░ Defined-By: systemd
    ░░ A start job for unit sshd.service has begun execution.
    ░░ The job identifier is 322.
    Dec 28 18:29:10 beedell.rokejulianlockhart.1669806072 sshd[1412]: Server listening on 0.0.0.0 port 22.
    Dec 28 18:29:10 beedell.rokejulianlockhart.1669806072 sshd[1412]: Server listening on :: port 22.

Footnotes

  1. MicrosoftDocs/PowerShell-Docs/blob/d2da3d7eede5c03b81c26f28d4ba5a67bc200a90/reference/docs-conceptual/security/remoting/SSH-Remoting-in-PowerShell.md#install-the-ssh-service-on-a-windows-computer ↩

  2. serverfault.com/revisions/858700/1 ↩

Activity

  1. added
    Issue-Enhancement
    Needs-Triage
    on Dec 28, 2022
  2. rhubarb-geek-nz commented on Feb 4, 2023

    @rhubarb-geek-nz

    ... and the potential solution being, I expect, so easy.

    It may be simple solution for this single case but it raises a whole new set of expectations that PowerShell will provide systems management functions on all platforms equivalent to what it does on windows.

    The term 'Restart-Service' is not recognized as a name of a cmdlet, function, script ...

    I would suggest a better solution would be to have new GitHub projects that implements the equivalent missing cmdlets for different platforms much like PowerShell/ThreadJob is a separate project. Given the platforms that PowerShell supports as a scripting language, that would imply there need to be equivalents for Linux as well as macOS, and for Linux for systemd, openrc or traditional init systems.

  3. SteveL-MSFT commented on Aug 2, 2023

    @SteveL-MSFT
    Member

    The @PowerShell/wg-powershell-cmdlets discussed this. We agreed that there is existing precedent to provide cmdlet abstraction over Unix commands (see Restart-Computer as an example).

    In this case, we recommend a fallback system to search for common commands used by various distros (including macOS) to restart a service on those platforms and if no common command is found then fail the entire operation,. We also suggest looking if sudo exists and using that to avoid the inevitable permission problem as sudo cannot elevate cmdlets. Check the exit code and if non-zero, emit STDERR as an ErrorRecord.

    We would expect Get-Service, Stop-Service and Start-Service to also use the same/similar code.

    For capability beyond what is provided by the existing cmdlet, we would recommend as a separate module published to PowerShellGallery.

  4. added
    WG-Reviewed
    and removed
    Needs-Triage
    on Aug 2, 2023
  5. added
    Resolution-No Activity
    on Jan 30, 2024
  6. 0009-0009-1013-986X commented on Jan 30, 2024

    @0009-0009-1013-986X
    Author
  7. removed
    Resolution-No Activity
    on Jan 30, 2024
  8. added
    Resolution-No Activity
    on Jul 29, 2024
  9. changed the title [-]Restart-Service should call systemctl restart on systemd-present Linux distributions.[/-] [+]`Restart-Service` should call `systemctl restart` on Linux OSes with SystemD present.[/+] on Jul 31, 2024
  10. 0009-0009-1013-986X commented on Jul 31, 2024

    @0009-0009-1013-986X
    Author
  11. removed
    Resolution-No Activity
    on Jul 31, 2024
  12. added
    Resolution-No Activity
    on Jan 28, 2025
  13. 0009-0009-1013-986X commented on Jan 28, 2025

    @0009-0009-1013-986X
    Author
  14. removed
    Resolution-No Activity
    on Jan 28, 2025
  15. added
    Resolution-No Activity
    on Aug 4, 2025
  16. cachius commented on Aug 4, 2025

    @cachius

    Steve Lee (@SteveL-MSFT) The commands would be

    • For Linux/systemd systemctl restart Stop and then start one or more units specified on the command line. If the units are not running yet, they will be started.

    • For macOS launchctl kickstart -k If the service is already running, kill the running instance before restarting the service.

    If going the way of Restart-Computer. One could also do a minimal cross-platform ServiceController. Maybe leaned on CIM.

  17. removed
    Resolution-No Activity
    on Aug 4, 2025
  18. added
    Resolution-No Activity
    on Feb 1, 2026
  19. 0009-0009-1013-986X commented on Feb 1, 2026

    @0009-0009-1013-986X
    Author
  20. removed
    Resolution-No Activity
    on Feb 1, 2026
  21. added
    Resolution-No Activity
    on Aug 1, 2026
  22. added
    Up-for-Grabs
    KeepOpen
    and removed
    Resolution-No Activity
    on Aug 1, 2026
  23. 0009-0009-1013-986X commented on Aug 2, 2026

    @0009-0009-1013-986X
    Author
  24. added
    In-PR
    on Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    In-PRIssue-EnhancementKeepOpenUp-for-GrabsWG-CmdletsWG-Reviewed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions