how can i spin down HDD or put drive offline / online using .bat file?


  1. Posts : 33
    win 10 pro 64-bit
       #1

    how can i spin down HDD or put drive offline / online using .bat file?


    Hiya, sorry if this has been discussed before, but i didn't find anything specific and nothing that would help.

    can someone please tell me how i can make a batch file that either spins down my HDD permanently (until i manually spin it up again) or simply put disk to offliine mode?

    i know i can do that via disk management, but that's too long. i need a simple batch file i can click on to quickly put drive offline / online.
    i did some research and some say use cmd and diskpart, one answer i found is:

    Command line to set online disk:

    DISKPART > list disk
    select disk x
    offline disk
    but this doesn't work for shit. i dont know which dark hole some people pull this info from. if i put this into batch file, it just makes an empty file on my desktop and nothing else. and yes, obvously i replaced "x" with the disk number that i want to put online/offline

    another post says use powershell script and put this in script:

    "select disk 2", "offline disk" | diskpart

    and to put it back online:

    "select disk 2", "online disk" | diskpart
    typically this doesn't work either.

    i also found someone saying:
    So, without creating any temporary script files you can call diskpart like this:

    PS C:\> $command = @'
    >> select disk 0
    >> offline disk
    >> '@
    >>
    PS C:\> $command | diskpart


    DISKPART>
    Disk 0 is now the selected disk.

    DISKPART>
    DiskPart successfully offlined the selected disk.

    DISKPART> PS C:\>
    but i have no idea how he can use this as a script. or he means literally without using a script? because topic was about USING A SCRIPT and not doing it manually, so i think he's confused as what was asked.


    anyway sorry for the long post, i just wanted to bring some examples. i know 100% its possible to do it via batch file, because i had it, but on some reason i deleted the batch files and i can't find them anywhere. i had 2 batch files, one to spin down HDD and other to spin it back up. if i remember right, it was not using diskpart, it literally spun down the drive without putting it offline, i was able to see the drive in windows, but it wasn't responding and i could hear the drive spin down.

    i might have been using some 3rd party cmd line tool, but it was years ago and i can't remember. so can someone please refresh my memory or provide any other way to achieve this?

    i'm just so tired of windows randomly spinning up my drives while i'm not using them, it really gets on my nerves. i have 5x4tb HDDs and windows keeps touching them when i'm not using them. like the other day i was playing a multiplayer game and all the sudden windows starts spinning up all the drives, i could hear them go one after another and it caused my game to freeze for a good 5 seconds, because of whatever windows was doing! this has really made me angry and i'm losing my patience.

    i hope someone can help me out. thanks in advance.
      My Computer


  2. Posts : 4,163
    Windows 11 Pro, 22H2
       #2

    I have your solution:

    First, it's important to understand that when you run diskpart from a batch file, the batch file pauses and waits for you to exit diskpart because you are calling an external program. So, once you run the command "diskpart" within the batch file, it simply pauses and the remaining commands are not run. When you exit from diskpart, the batch file will attempt to run the additional commands, which obviously doesn't work because those commands need to run WITHIN diskpart.

    There are two ways to solve this:

    1) Diskpart supports the use of an external script file. You would place all of the commands that you want to have diskpart run in that script file and then use the command diskpart /s scriptname.txt within your batch file to execute the diskpart commands. If you want to create a logfile to see the output from diskpart, you could do this: diskpart /s scriptname.txt > logfile.txt.

    Reference: https://learn.microsoft.com/en-us/wi...s-and-examples

    2) My preferred method: I have created some batch files that run diskpart commands, but the disk I am referencing can change so it's impractical to use a script file. I need to be able to change the disk number and other parameters using variables in the batch file. Also, I just hate having to manage a whole pile of external script files. So, here is a method that let's you run all the commands from a single batch file and allows additional versatility (which I won't cover in detail here). Let's start with a simple example, with explanation following the example:

    Code:
    (echo select disk 1
    echo offline disk
    echo exit
    ) | diskpart
    This batch file echos all of the diskpart commands. The "echo" command would normally just echo or display that text to the screen. For example, echo hello would simply display "hello" on the screen. However, note that all of the echo commands are grouped together by combining them using parenthesis and then on the last line we redirect or "pipe" the output of those echo commands to the diskpart command.

    So, to use this, start by writng all of the commands you want run in diskpart like this:

    Code:
    select disk 1
    offline disk
    exit
    Add the word echo to the start of the each line like this:

    Code:
    echo select disk 1
    echo offline disk
    echo exit
    Add an open parenthesis to the start of the first line like this:

    Code:
    (echo select disk 1
    echo offline disk
    echo exit
    Add one additional line that says ) | diskpart like this:

    Code:
    (echo select disk 1
    echo offline disk
    echo exit
    ) | diskpart
    Done!

    IMPORTANT: Diskpart needs to be run elevated, so you should make sure to right-click the batch file and choose to "Run as administrator". Of course, you can automate that as well if you want like this:

    NOTE: The first line simply checks to see if the batch file is being run as Administrator, if it is not, it re-runs the batch file as administrator and terminates the initial batch file that was not run as administrator.

    Code:
    (Fsutil Dirty Query %SystemDrive%>Nul)||(PowerShell start """%~f0""" -verb RunAs & Exit /B)
    
    (echo select disk 1
    echo offline disk
    echo exit
    ) | diskpart
    If anything is not clear, or if you need any further assistance, please do let me know.
      My Computers


  3. Posts : 832
    Windows 7
       #3

    PowerShell makes this task very simple. As Admin, open a new window and run:
    Code:
    powershell Set-Disk 1 -IsOffline $true
    powershell Set-Disk 1 -IsOffline $false
    powershell "(Get-Disk 1,2,3,4) | Set-Disk -IsOffline $true"
    Where the disk number matches what diskpart reports.

    Now the problem is you have to watch that Windows doesn't renumber disks. Which isn't a concern while you're playing a game, but it might change after you unplug drives, add or remove disk volumes.

    Personally, I want to specify drive letters instead of using disk numbers. This requires a PS script which reads HKLM\SYSTEM\MountedDevices and looks for each drive's GPT GUID. With the GUID, we can reference its disk number and can spin drives up/down based on drive letter.

    The one problem is this doesn't work for MBR drives. After a MBR drive is unmounted, Windows doesn't preserve the MBR signature in the Volume or Partition data, and you don't know which drive it was. Technically you could write this to file or registry -- but most users don't use MBR these days, and hardly on non-boot drives.

    SwitchDrive.bat takes a list of drive letters, and two optional arguments.
    Code:
    C:\Users\GARLIN\Downloads>SwitchDrive.bat on E G
    Disk 1 (F: G:) is online.
    Disk 2 (E:) is online.
    
    C:\Users\GARLIN\Downloads>SwitchDrive.bat off E G
    Disk 1 (F: G:) is offline.
    Disk 2 (E:) is offline.
    
    C:\Users\GARLIN\Downloads>SwitchDrive.bat G
    Disk 1 (F: G:) is online.
    Only drives labeled as GPT will be processed. All other drive types (including MBR) are silently ignored.
    C drive is always ignored. For obvious reasons!

    Specifying "on" forces all the listed drives to be online.
    Specifying "off" forces all listed drives to be offline.

    Without using "on" or "off", every drive is swapped to the mirror image of their current status.

    When SwitchDrive.bat makes a change, it reports both the disk number and all drive letters mounted on that disk (if you have multiple volumes on the same disk). Nothing is written to any disk.
    how can i spin down HDD or put drive offline / online using .bat file? Attached Files
      My Computer


  4. Posts : 1,201
    11 Home
       #4

    Windows 10 has an annoying tendency to randomly wake up a sleeping HDD regardless of whether you took the HDD offline before the spindown happened. This free tool used to work perfectly fine for years:
    revoSleep - Get revoSleep and let sleep your hard disk individually!
    But as you can see for yourself on the forum section of that website, there have been multiple reports from users saying their HDD still wakes up inadvertently some time after they put their HDD to sleep with revoSleep. I have been experiencing this same exact problem for many years until finally, at very long last, I found the fix that worked. If you're in the same boat, then you can try following the steps below.

    First, you'll need an additional free tool called HDDScan for Windows. This tool is portable so I just put it in a folder C:\tools\HDDScan and, if you, like me, have a 4K screen so that the font in this program is microscopically small, then on the Compatibility tab of the Properties panel for the shortcut (HDDScan.lnk) you can click on the button for high DPI settings that appears near the bottom so there you can change the setting Override high DPI scaling behavior, scaling performed by: Application.

    Next, assuming that you have already installed revoSleep, look for these two batchfiles in the installation folder of revoSleep (by default, that would be in C:\Program Files\revoSleep v0.4 ). Use notepad to edit these two batchfiles and replace their content like so:

    beforeSleep.bat
    Code:
    "C:\tools\HDDScan\HDDScan.exe" %3 -PM 1
    afterSleep.bat
    Code:
    @ECHO OFF
    SET c=15
    :FindDrive
    IF EXIST "%~1\" GOTO Mounted
    TIMEOUT /T 2
    SET /A c-=1
    IF %c%==0 EXIT
    GOTO FindDrive
    :Mounted
    "C:\tools\HDDScan\HDDScan.exe" %3 -PM 0
    Please note, the 0 at the end of the 2nd batchfile represents the idle timeout value for the HDD you want to be set each time after you wake the HDD up again with revoSleep. Also please note, rebooting or waking the computer from S3 Standby will reset this value back to its default setting which is hardware-specific so it depends on the manufacturer/model of the HDD in question. (On my 8TB Seagate Barracuda if I set this value to 0 it means that the idle timeout is set to maximum.) The lowest possible timeout is 1. So basically before revoSleep takes the HDD offline and deactivates it, it runs beforeSleep.bat which sets the idle timeout to 1 causing the HDD to fall asleep a second or two AFTER it's taken offline and deactivated. For this trick to work correctly all you need to do is, in revoSleep, click on Show Details for the HDD in question and make sure to untick Sleep and make sure that both Offline and Deactivate are ticked.

    Hopefully this will help you.
      My Computers


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 04:10.
Find Us




Windows 10 Forums