Search code examples
c++systemstdsystem-shutdownsystem-restore

Shutting down and Restarting PC via C++ in WSL


Here is my program to shutdown the PC in c++, and I use vs code editor and WSL to run this program:

          #include<iostream>
          #include<stdlib.h>
          int main()
          {

             system("C:\\Windows\\System32\\shutdown /i ");

          }

I got this message sh: 1: C:WindowsSystem32shutdown: not found.


Solution

  • Make sure you use the appropriate path. The correct form for WSL via Linux is "/mnt/c/Windows/System32/shutdown.exe" as mentioned by prog-fh and code_fodder.

    So this will work: (I haven't tested it in WSL, but the above users did and know better)

    std::system("/mnt/c/Windows/System32/shutdown.exe /i");
    

    or for shutdown you can use s as well:

    std::system("/mnt/c/Windows/System32/shutdown.exe /s"); 
    

    Likewise, for restarting, use r:

    std::system("/mnt/c/Windows/System32/shutdown.exe /r");