Ever needed to make a PC Shutdown, Reboot, Hibernate, … after running some code?
As with most things, there are a number of possible approaches, but today I thought I’d demonstrate a very simple solution using the Shutdown command:

Below are a few simple procedures to do individual actions:
Logoff
Public Function PC_LogOff() As Boolean
Dim vRetVal As Variant
vRetVal = Shell("shutdown /l /f")
If vRetVal <> 0 Then PC_LogOff = True
End Function
Hibernate
Public Function PC_Hibernate() As Boolean
Dim vRetVal As Variant
vRetVal = Shell("shutdown /h")
If vRetVal <> 0 Then PC_Hibernate = True
End Function
Restart
Public Function PC_Restart() As Boolean
Dim vRetVal As Variant
vRetVal = Shell("shutdown /r /f")
If vRetVal <> 0 Then PC_Restart = True
End Function
Shutdown
Public Function PC_Shutdown() As Boolean
Dim vRetVal As Variant
vRetVal = Shell("shutdown /s /f")
If vRetVal <> 0 Then PC_Shutdown = True
End Function
One Function to Rule Them All
Knowing the above, we could always combine things into a single versatile function like:
Enum PC_ShutdownCommand
Shutdown_LogOff = 1
Shutdown_Hibernate = 2 'has to be enabled on the system to work!
Shutdown_Restart = 3
Shutdown_Shutdown = 4
End Enum
'---------------------------------------------------------------------------------------
' Procedure : PC_Shutdown2
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website : http://www.cardaconsultants.com
' Purpose : Initiate one of the Shutdown commands (hybernate, restart, shutdown, logoff)
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: Late Binding -> None required
' References:
' https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/shutdown
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' lCmd : PC_ShutdownCommand Enum value of the command to issue
'
' Usage:
' ~~~~~~
' PC_Shutdown2(Shutdown_Hibernate)
' Returns -> True is successful
' False is the command couldn't be executed for some reason
'
' Revision History:
' Rev Date(yyyy-mm-dd) Description
' **************************************************************************************
' 1 2024-04-13 Initial Release
' Added Procedure header and error handler
'---------------------------------------------------------------------------------------
Function PC_Shutdown2(lCmd As PC_ShutdownCommand) As Boolean
On Error GoTo Error_Handler
Dim sCmd As String
Dim vRetVal As Variant
Select Case lCmd
Case Shutdown_LogOff
sCmd = "/l /f"
Case Shutdown_Hibernate
sCmd = "/h"
Case Shutdown_Restart
sCmd = " /r /f"
Case Shutdown_Shutdown
sCmd = "/s /f"
End Select
If sCmd <> "" Then
vRetVal = Shell("shutdown " & sCmd)
If vRetVal <> 0 Then PC_Shutdown2 = True
End If
Error_Handler_Exit:
On Error Resume Next
Exit Function
Error_Handler:
MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
"Error Source: PC_Shutdown2" & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occurred!"
Resume Error_Handler_Exit
End Function
More Options
Do note that the Shutdown command offers a multitude of parameters. In my examples I have kept things very simple, but you can easily add delays, reboot into options menu, …
So use this as a starting point and feel free to take things further to meet your project needs.