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. 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.

  2. 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.

  3. added
    WG-ReviewedA Working Group has reviewed this and made a recommendation
    and removed
    Needs-TriageThe issue is new and needs to be triaged by a work group.
    on Aug 2, 2023
  4. beedell-roke commented on Jan 30, 2024

    @beedell-roke
    Author
  5. 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
  6. beedell-roke commented on Jul 31, 2024

    @beedell-roke
    Author
  7. beedell-roke commented on Jan 28, 2025

    @beedell-roke
    Author
  8. 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.

  9. beedell-roke commented on Feb 1, 2026

    @beedell-roke
    Author
  10. added
    Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributors
    KeepOpenThe bot will ignore these and not auto-close
    and removed
    Resolution-No ActivityIssue has had no activity for 6 months or more
    on Aug 1, 2026
  11. beedell-roke commented on Aug 2, 2026

    @beedell-roke
    Author
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-PRIndicates that a PR is out for the issueIssue-Enhancementthe issue is more of a feature request than a bugKeepOpenThe bot will ignore these and not auto-closeUp-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Cmdletsgeneral cmdlet issuesWG-ReviewedA Working Group has reviewed this and made a recommendation

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions