Solved Batch file to put computer to sleep


glasskuter

aka Mama Glass
Guru
VIP
Local time
2:18 AM
Posts
7,968
Location
The Lone Star State of Texas
OS
Windows 11 Pro 24H2 2600.1742
Calling on all you cmd line gurus out there...I'm not too sharp using the cmd window. I have tried various methods to make a shortcut to sleep my PC. I finally got it to work by making a batch file and then making a shortcut to the batch file that I pinned to the taskbar. My problem is, the cmd window is left open after the cmd runs. Not too big of a deal but it irks me. What do I need to add at the end to make cmd automatically close after it runs. Here's the batch file.

powercfg -h off
rundll32.exe powrprof.dll,SetSuspendState 0,1,0
powercfg -h on
 

My Computers

System One System Two

  • OS
    Windows 11 Pro 24H2 2600.1742
    Computer type
    PC/Desktop
    Manufacturer/Model
    Dell Optiplex 7080
    CPU
    i9-10900 10 core 20 threads
    Motherboard
    DELL 0J37VM
    Memory
    32 gb
    Graphics Card(s)
    none-Intel UHD Graphics 630
    Sound Card
    Integrated Realtek
    Monitor(s) Displays
    Benq 27
    Screen Resolution
    2560x1440
    Hard Drives
    1tb Solidigm m.2 nvme+256gb SKHynix m.2 nvme /External +512gb Samsung m.2 sata+1tb Kingston m2.nvme
    PSU
    500w
    Case
    MT
    Cooling
    Dell Premium
    Keyboard
    Logitech wired
    Mouse
    Logitech wireless
    Internet Speed
    so slow I'm too embarrassed to tell
    Browser
    Firefox
    Antivirus
    Defender+MWB Premium
  • Operating System
    Windows 10 Pro 22H2 19045.3930
    Computer type
    PC/Desktop
    Manufacturer/Model
    Dell Optiplex 9020
    CPU
    i7-4770
    Memory
    24 gb
    Monitor(s) Displays
    Benq 27
    Screen Resolution
    2560x1440
    Hard Drives
    256 gb Toshiba BG4 M.2 NVE SSB and 1 tb hdd
    PSU
    500w
    Case
    MT
    Cooling
    Dell factory
    Mouse
    Logitech wireless
    Keyboard
    Logitech wired
    Internet Speed
    still not telling
    Browser
    Firefox
    Antivirus
    Defender+MWB Premium
add exit at the end
 

My Computer

System One

  • OS
    Windows 11 21H2 Home Single Language
    Computer type
    Laptop
    Manufacturer/Model
    Hometech ALFA400C
    CPU
    I'm ashamed to talk about it...
    Motherboard
    There is no such a God, I only know mother-Earth
    Memory
    3GB :(
    Graphics Card(s)
    on-board
    Sound Card
    on-board
    Monitor(s) Displays
    (s) is too ambitious for me
    Screen Resolution
    1920*1080
    Hard Drives
    Samsung SSD 860 EVO 250GB (SSD)
    Samsung MBG4GC 32GB (SSD)
    PSU
    I was very young when I last saw a PSU
    Case
    ...
    Cooling
    My coldness is enough to cool the computer
    Keyboard
    A broken and very clean keyboard
    Mouse
    Rat King
    Internet Speed
    Slowest internet connection in Europe
    Browser
    ChromePortable / TorBrowser
    Antivirus
    Biontech
    Other Info
    I can simply say that I took the red pill and I am not happy with it.

My Computers

System One System Two

  • OS
    Windows 11 Pro 24H2 2600.1742
    Computer type
    PC/Desktop
    Manufacturer/Model
    Dell Optiplex 7080
    CPU
    i9-10900 10 core 20 threads
    Motherboard
    DELL 0J37VM
    Memory
    32 gb
    Graphics Card(s)
    none-Intel UHD Graphics 630
    Sound Card
    Integrated Realtek
    Monitor(s) Displays
    Benq 27
    Screen Resolution
    2560x1440
    Hard Drives
    1tb Solidigm m.2 nvme+256gb SKHynix m.2 nvme /External +512gb Samsung m.2 sata+1tb Kingston m2.nvme
    PSU
    500w
    Case
    MT
    Cooling
    Dell Premium
    Keyboard
    Logitech wired
    Mouse
    Logitech wireless
    Internet Speed
    so slow I'm too embarrassed to tell
    Browser
    Firefox
    Antivirus
    Defender+MWB Premium
  • Operating System
    Windows 10 Pro 22H2 19045.3930
    Computer type
    PC/Desktop
    Manufacturer/Model
    Dell Optiplex 9020
    CPU
    i7-4770
    Memory
    24 gb
    Monitor(s) Displays
    Benq 27
    Screen Resolution
    2560x1440
    Hard Drives
    256 gb Toshiba BG4 M.2 NVE SSB and 1 tb hdd
    PSU
    500w
    Case
    MT
    Cooling
    Dell factory
    Mouse
    Logitech wireless
    Keyboard
    Logitech wired
    Internet Speed
    still not telling
    Browser
    Firefox
    Antivirus
    Defender+MWB Premium
Create a VBS Script:

Code:
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "PATH TO YOUR BATCH SCRIPT" & Chr(34), 0
Set WshShell = Nothing

Replace PATH TO YOUR BATCH SCRIPT with the actual path to your file you wanna run.

Example:
1636302223642.png

Contens of writetext.bat:
1636302275703.png

Result every time I click the VBS file:
1636302321071.png

Contens of text.txt after a few clicks:
1636302369812.png

Never was any CMD windows displayed on my system. Try it out. 😄
 

My Computers

System One System Two

  • OS
    Windows 11 Pro 23H2 build 10.0.22631.3296 (Release Channel) / Linux Mint 21.3 Cinnamon
    Computer type
    Laptop
    Manufacturer/Model
    Lenovo A485
    CPU
    Ryzen 7 2700U Pro
    Motherboard
    Lenovo (WiFi/BT module upgraded to Intel Wireless-AC-9260)
    Memory
    32GB
    Graphics Card(s)
    iGPU Vega 10
    Sound Card
    Realtek
    Monitor(s) Displays
    14" FHD (built-in) + 14" Lenovo Thinkvision M14t (touch+pen) + 32" Asus PB328
    Screen Resolution
    FHD + FHD + 1440p
    Hard Drives
    Intel 660p m.2 nVME PCIe3.0 x2 512GB
    PSU
    65W
    Keyboard
    Thinkpad / Logitech MX Keys
    Mouse
    Logitech MX Master 2S
    Internet Speed
    600/300Mbit
    Browser
    Edge (Chromium)
    Antivirus
    Windows Defender
    Other Info
    SecureBoot: Enabled
    TPM2.0: Enabled
    AMD-V: Enabled
  • Operating System
    Windows 11 Pro 23H2 build 10.0.22631.3296(Release Preview Channel)
    Computer type
    PC/Desktop
    Manufacturer/Model
    Custom
    CPU
    i7-7700k @4.8GHz
    Motherboard
    Asus PRIME Z270-A
    Memory
    32GB 2x16GB 2133MHz CL15
    Graphics card(s)
    EVGA GTX1080Ti FTW 11GB
    Sound Card
    Integrated
    Monitor(s) Displays
    32" 10-bit Asus PB328Q
    Screen Resolution
    WQHD 2560x1440
    Hard Drives
    512GB ADATA SX8000NP NVMe PCIe Gen 3 x4
    PSU
    850W
    Case
    Fractal Design Define 7
    Cooling
    Noctua NH-D15 chromax.black
    Mouse
    Logitech MX Master 2S
    Keyboard
    Logitech MX Keys
    Internet Speed
    600/300Mbit
    Browser
    Edge (Cromium)
    Antivirus
    Windows Defender
    Other Info
    AC WiFi Card

My Computers

System One System Two

  • OS
    Windows 11 Pro 24H2 2600.1742
    Computer type
    PC/Desktop
    Manufacturer/Model
    Dell Optiplex 7080
    CPU
    i9-10900 10 core 20 threads
    Motherboard
    DELL 0J37VM
    Memory
    32 gb
    Graphics Card(s)
    none-Intel UHD Graphics 630
    Sound Card
    Integrated Realtek
    Monitor(s) Displays
    Benq 27
    Screen Resolution
    2560x1440
    Hard Drives
    1tb Solidigm m.2 nvme+256gb SKHynix m.2 nvme /External +512gb Samsung m.2 sata+1tb Kingston m2.nvme
    PSU
    500w
    Case
    MT
    Cooling
    Dell Premium
    Keyboard
    Logitech wired
    Mouse
    Logitech wireless
    Internet Speed
    so slow I'm too embarrassed to tell
    Browser
    Firefox
    Antivirus
    Defender+MWB Premium
  • Operating System
    Windows 10 Pro 22H2 19045.3930
    Computer type
    PC/Desktop
    Manufacturer/Model
    Dell Optiplex 9020
    CPU
    i7-4770
    Memory
    24 gb
    Monitor(s) Displays
    Benq 27
    Screen Resolution
    2560x1440
    Hard Drives
    256 gb Toshiba BG4 M.2 NVE SSB and 1 tb hdd
    PSU
    500w
    Case
    MT
    Cooling
    Dell factory
    Mouse
    Logitech wireless
    Keyboard
    Logitech wired
    Internet Speed
    still not telling
    Browser
    Firefox
    Antivirus
    Defender+MWB Premium
Awesome! Happy to help. 😄
 

My Computers

System One System Two

  • OS
    Windows 11 Pro 23H2 build 10.0.22631.3296 (Release Channel) / Linux Mint 21.3 Cinnamon
    Computer type
    Laptop
    Manufacturer/Model
    Lenovo A485
    CPU
    Ryzen 7 2700U Pro
    Motherboard
    Lenovo (WiFi/BT module upgraded to Intel Wireless-AC-9260)
    Memory
    32GB
    Graphics Card(s)
    iGPU Vega 10
    Sound Card
    Realtek
    Monitor(s) Displays
    14" FHD (built-in) + 14" Lenovo Thinkvision M14t (touch+pen) + 32" Asus PB328
    Screen Resolution
    FHD + FHD + 1440p
    Hard Drives
    Intel 660p m.2 nVME PCIe3.0 x2 512GB
    PSU
    65W
    Keyboard
    Thinkpad / Logitech MX Keys
    Mouse
    Logitech MX Master 2S
    Internet Speed
    600/300Mbit
    Browser
    Edge (Chromium)
    Antivirus
    Windows Defender
    Other Info
    SecureBoot: Enabled
    TPM2.0: Enabled
    AMD-V: Enabled
  • Operating System
    Windows 11 Pro 23H2 build 10.0.22631.3296(Release Preview Channel)
    Computer type
    PC/Desktop
    Manufacturer/Model
    Custom
    CPU
    i7-7700k @4.8GHz
    Motherboard
    Asus PRIME Z270-A
    Memory
    32GB 2x16GB 2133MHz CL15
    Graphics card(s)
    EVGA GTX1080Ti FTW 11GB
    Sound Card
    Integrated
    Monitor(s) Displays
    32" 10-bit Asus PB328Q
    Screen Resolution
    WQHD 2560x1440
    Hard Drives
    512GB ADATA SX8000NP NVMe PCIe Gen 3 x4
    PSU
    850W
    Case
    Fractal Design Define 7
    Cooling
    Noctua NH-D15 chromax.black
    Mouse
    Logitech MX Master 2S
    Keyboard
    Logitech MX Keys
    Internet Speed
    600/300Mbit
    Browser
    Edge (Cromium)
    Antivirus
    Windows Defender
    Other Info
    AC WiFi Card
Does rundll32.exe powrprof.dll,SetSuspendState 0,1,0 still work? I wanted to create a simple shortcut, and several web pages for Win10 said that that command line from cmd.exe (as administrator) should sleep (or hibernate, if that's enabled) the computer, but it doesn't seem to do anything now in Win11.

I also tried creating a .bat file with the .vbs script as above, and again nothing seems to happen.
 

My Computers

System One System Two

  • OS
    Windows 11 version 21H2 (OS Build 22000.469)
    Computer type
    Laptop
    Manufacturer/Model
    HP Spectre X360 14
    CPU
    i7-1195G7
    Memory
    16GB
    Monitor(s) Displays
    external 24" Dell P2415Q
    Screen Resolution
    3840 x 2160 on external Dell, 3000 x 2000 on laptop screen
    Hard Drives
    1TB SSD with 32GB Optane cache
    Browser
    Vivaldi, Comodo Dragon, Edge
    Antivirus
    Avast (free version)
  • Operating System
    Windows 11 Pro, 21H2 (Build 22000.376)
    Computer type
    Tablet
    Manufacturer/Model
    Surface Pro 6
    CPU
    i7-8650U
    Memory
    16 GB
    Graphics card(s)
    iGPU (Intel® UHD Graphics 620)
    Monitor(s) Displays
    Dell P2415Q
    Screen Resolution
    3840 x 2160 on external Dell, 2736 x 1824 on SP6 screen
    Hard Drives
    512GB PCIe Gen 3 x2 SSD
    Browser
    Vivaldi, Comodo Dragon, Edge
    Antivirus
    Avast (free edition)
I'm running Win11 22H2 with Hibernate disabled. I have a shortcut on the desktop named "Sleep" with a target of :
C:\Windows\System32\rundll32.exe powrprof.dll,SetSuspendState 0,1,0

Double clicking this shortcut puts the computer to sleep, no batch file needed.
 

My Computers

System One System Two

  • OS
    Windows 11 Pro
    Computer type
    PC/Desktop
    Manufacturer/Model
    Home Built
    CPU
    Ryzen 5 5600G
    Motherboard
    Gigabyte B550 Aorus Pro AC
    Memory
    Ripjaws 2x8GB 3600
    Graphics Card(s)
    CPU internal
    Monitor(s) Displays
    Sharp LC-32D64U
    Screen Resolution
    1920 x 1080
    Hard Drives
    WD_Black SN770 250GB
    PSU
    Antec Earthwatts 380w
    Case
    Antec Sonata
    Cooling
    Scythe Mugen
    Keyboard
    Microsoft KU-0459
    Mouse
    Gaming Mouse (eBay)
    Internet Speed
    500/500
    Browser
    Firefox
    Antivirus
    Windows Defender
    Other Info
    Low noise, low power dissipation
  • Operating System
    Windows 10 Pro
    Computer type
    PC/Desktop
    Manufacturer/Model
    Home Built
    CPU
    AMD 5350
    Motherboard
    ASUS AM1I-A
    Memory
    8GB
    Monitor(s) Displays
    Dell
    Screen Resolution
    1920 x 1080
    Hard Drives
    Kingston 120GB SSD
    PSU
    Antec Earthwatts 380W
    Case
    Antec NSK 3180
    Cooling
    AMD
    Mouse
    Dell
    Keyboard
    Dell
    Internet Speed
    500/500
    Browser
    Firefox
    Antivirus
    Windows Defender
@Very good @GadgetBuilder That works too and is much easier. Thanks for the enlightenment.
 

My Computers

System One System Two

  • OS
    Windows 11 Pro 24H2 2600.1742
    Computer type
    PC/Desktop
    Manufacturer/Model
    Dell Optiplex 7080
    CPU
    i9-10900 10 core 20 threads
    Motherboard
    DELL 0J37VM
    Memory
    32 gb
    Graphics Card(s)
    none-Intel UHD Graphics 630
    Sound Card
    Integrated Realtek
    Monitor(s) Displays
    Benq 27
    Screen Resolution
    2560x1440
    Hard Drives
    1tb Solidigm m.2 nvme+256gb SKHynix m.2 nvme /External +512gb Samsung m.2 sata+1tb Kingston m2.nvme
    PSU
    500w
    Case
    MT
    Cooling
    Dell Premium
    Keyboard
    Logitech wired
    Mouse
    Logitech wireless
    Internet Speed
    so slow I'm too embarrassed to tell
    Browser
    Firefox
    Antivirus
    Defender+MWB Premium
  • Operating System
    Windows 10 Pro 22H2 19045.3930
    Computer type
    PC/Desktop
    Manufacturer/Model
    Dell Optiplex 9020
    CPU
    i7-4770
    Memory
    24 gb
    Monitor(s) Displays
    Benq 27
    Screen Resolution
    2560x1440
    Hard Drives
    256 gb Toshiba BG4 M.2 NVE SSB and 1 tb hdd
    PSU
    500w
    Case
    MT
    Cooling
    Dell factory
    Mouse
    Logitech wireless
    Keyboard
    Logitech wired
    Internet Speed
    still not telling
    Browser
    Firefox
    Antivirus
    Defender+MWB Premium
I guess it must be tedious going to the Start menu and choosing Sleep :-)

Anyway, for those with keyboards with assignable keys, and the associated software, you can choose a button to put the PC to sleep. Here I have a Logitech Craft Keyboard with a "Lock" key, but instead I've assigned it to "Sleep" using the Logitech Options software...

2022-10-22_11-54-09.jpg

Now I can simply press that button and the PC will sleep. But, yes, I know the post is about creating a batch file, so. Just thought I'd throw this out for those who looking for other options (no pun intended).
 

My Computers

System One System Two

  • OS
    Windows 11 Pro 23H2 (Build 22631.4249)
    Computer type
    PC/Desktop
    Manufacturer/Model
    Custom built
    CPU
    Intel i9-9900K
    Motherboard
    Gigabyte Aorus Z390 Xtreme
    Memory
    32G (4x8) DDR4 Corsair RGB Dominator Platinum (3600Mhz)
    Graphics Card(s)
    Radeon (XFX MERC 310) RX 7900XT
    Sound Card
    Onboard (ESS Sabre HiFi using Realtek drivers)
    Monitor(s) Displays
    27-inch Eizo Color Edge - CG2700X
    Screen Resolution
    3840 x 2160
    Hard Drives
    5 Samsung SSD drives: 2X 970 NVME (512 & 1TB), 3X EVO SATA (2X 2TB, 1X 1TB)
    PSU
    EVGA Super Nova I000 G2 (1000 watt)
    Case
    Cooler Master H500M
    Cooling
    Corsair H115i Elite Capellix XT
    Keyboard
    Logitech Craft
    Mouse
    Logitech MX Master 3
    Internet Speed
    500mb Download. 11mb Upload
    Browser
    Microsoft Edge Chromium
    Antivirus
    Windows Security
    Other Info
    System used for gaming, photography, music, school.
  • Operating System
    Win 11 Pro 23H2 (build 22631.4112)
    Computer type
    Laptop
    Manufacturer/Model
    Lenovo ThinkPad X1 Carbon (Gen 12)
    CPU
    Intel Core Ultra 7 165U vPro® Processor
    Motherboard
    Vendor
    Memory
    32 GB LPDDR5X-6400MHz (Soldered)
    Graphics card(s)
    Intel Graphics
    Sound Card
    Onboard
    Monitor(s) Displays
    14" 2.8K OLED, Anti Reflection, Touch, HDR 500, 400 nits, 120Hz
    Screen Resolution
    2880 x 1800
    Hard Drives
    1 TB SSD M.2 2280 PCIe Gen4 Performance TLC Opal
    PSU
    Vendor
    Case
    Lenovo
    Cooling
    Vapor Chamber Cooling
    Mouse
    Touchpad: Haptic Touchpad
    Keyboard
    Backlit, Black with Fingerprint Reader and WWAN
    Internet Speed
    100MB
    Browser
    Edge Chromium
    Antivirus
    Windows Security
    Other Info
    202. Build Your Own laptop.
    vPro Certified Model: vPro Enterprise
Back
Top Bottom