Powershell: Add some shutdown.exe functionality into Restart-Computer/Stop-Computer

Created on 11 Mar 2020  路  5Comments  路  Source: PowerShell/PowerShell

Summary of the new feature/enhancement

Over the last year or two, shutdown.exe has gained several new and worthwhile options (/g, /sg, /fw, etc.) which aren鈥檛 present in restart-computer. I would like to see these implemented in Restart-Computer (especially the three I mentioned).

The /g option (reboot with auto-restart) makes reboots easier. The /sg option (shutdown with auto-restart) makes shutdowns followed by restarts easier. The /fw option (reboot to firmware) solves the age-old problem of "which special key do I hit to get to BIOS/UEFI???"

Proposed technical implementation details (optional)

I don't know how the new options are implemented. I presume some Win32 call.

But for PowerShell, some new parameters such as [-Reboot] [-AutoRestart] [-Shutdown] [-Firmware] in appropriate parametersets would be desirable.

Oops.... TIL about Stop-Computer. I didn't even realize Stop-Computer existed until I was reading about #4857 (which leads to the question, what happened to #8112)?

So I guess I'm actually asking for [-AutoRestart] and [-Firmware] to be implemented in both Restart-Computer and Stop-Computer.

Area-Cmdlets-Management Issue-Question Up-for-Grabs

Most helpful comment

ExitWindowsEx() works just fine and exposes all of the expected restart options shown by MSDN.

With a P/Invoke/Add-Type I was able to do everything:

$Win32 = Add-Type -Name 'Win32' `
    -Namespace 'Win32' `
    -MemberDefinition @" 
        [DllImport( "user32.dll" )] 
        public static extern int ExitWindowsEx( int uFlags, int dwReason ); 
"@ -PassThru 

It appears that WindowsShutdown() and WindowsShutdownTracker() mask and suppress several of the ExitWindowsEx() options. I don't know why - but I bet there are comments in the Win32 sources.

But not that anything is a non-public API using ExitWindowsEx instead of ShutdownWindows*.

All 5 comments

Far be it from me to claim to be a programmer (I'm not, I'm just an admin scripter).

But according to Win32ShutdownTracker, it's implemented using ExitWindowsEx():

PS C:\scripts> get-cimclass -class win32_operatingsystem | 
    select -expand cimclassmethods |? { $_.Name -eq 'Win32ShutdownTracker' } |
    select -expand qualifiers |? { $_.Name -eq 'mappingstrings' } | 
    select Value

Value
-----
{Win32API|System Shutdown Functions|ExitWindowsEx}

AutoRestart (and Reboot and Shutdown) is fully defined within ExitWindowsEx(). I would guess the Win32ShutdownTracker doc was not updated with EWX_RESTARTAPPS.

Insofar as how reboot to BIOS/UEFI is done - I'm not sure. I would expect it to be another parameter to ExitWindowsEx() (there are 3 others defined in WinUser.h: EWX_QUICKRESOLVE, EWX_HYBRID_SHUTDOWN, and EWX_BOOTOPTIONS but these are not explicitly documented). EWX_BOOTOPTIONS seems likely.

Since both shutdown.exe and the Windows 10 UI know how to do a "reboot to BIOS/UEFI", it can't be too much of a secret (UI: Start --> Settings --> Update & Security --> Recovery --> Advanced Startup).

I can experiment with those 3 options.

It's also entirely possible that the option, is not a public API.

ExitWindowsEx() works just fine and exposes all of the expected restart options shown by MSDN.

With a P/Invoke/Add-Type I was able to do everything:

$Win32 = Add-Type -Name 'Win32' `
    -Namespace 'Win32' `
    -MemberDefinition @" 
        [DllImport( "user32.dll" )] 
        public static extern int ExitWindowsEx( int uFlags, int dwReason ); 
"@ -PassThru 

It appears that WindowsShutdown() and WindowsShutdownTracker() mask and suppress several of the ExitWindowsEx() options. I don't know why - but I bet there are comments in the Win32 sources.

But not that anything is a non-public API using ExitWindowsEx instead of ShutdownWindows*.

I was able to do everything

I don't see an option for -Firmware. Please update the issue description for latest status.

Was this page helpful?
0 / 5 - 0 ratings