Change drive letters from command line via a script

Page 1 of 4 123 ... LastLast

  1. Posts : 52
    Windows 10
       #1

    Change drive letters from command line via a script


    I have to manage dozens of PCs which includes a clean Windows install every 6 to 12 months.

    On every PC I have to use disk management to change drive letters. Windows doesn't assign the letters I want for each drive so I gotz to change them manually every time.

    Optical drive letters are always A: and B:

    Provided that no drives or partitions are added or removed then the drive letters for a PC are static.

    Is there a way I can script this so that I can automate it so I don't have to manually do it every time.

    An example might be I want to change drive E: to D: and then J: to A: and F: to E:
      My Computer


  2. Posts : 777
    Windows 7
       #2

    PowerShell is the best way to write it, here's a short CMD script which changes the drive letters but also performs sanity checks on the source and destination drive letters. Obviously you need to be Administrator.

    ChangeDrive.bat
    Code:
    <# : batch script
    @echo off
    powershell -nop Invoke-Expression ('$OldDrive = """"%1""""; $NewDrive = """"%2""""' + [System.IO.File]::ReadAllText('%~f0'))
    goto :eof
    #>
    
    if ((Get-Partition -DriveLetter $OldDrive -ErrorAction Ignore) -eq $null) {
        "{0}: is not assigned." -f $OldDrive
        exit 0
    }
    
    if ((Get-Partition -DriveLetter $NewDrive -ErrorAction Ignore) -ne $null) {
        "{0}: is unavailable." -f $NewDrive
        exit 0
    }
    
    try {
        Set-Partition -DriveLetter $OldDrive -NewDriveLetter $NewDrive
        "Changed {0}: to {1}:" -f $OldDrive, $NewDrive
    }
    catch {
        $_.Exception.Message
        exit 1
    }

    usage: ChangeDrive.bat F E
      My Computer


  3. Posts : 9,791
    Mac OS Catalina
       #3
      My Computer


  4. Posts : 52
    Windows 10
    Thread Starter
       #4

    Thank you for the script. I get these errors;

    Code:
    I:\PostInstall>chgdrive L Z
    L: is not assigned.
    This is correct. There is no L: drive.

    Code:
    I:\PostInstall>chgdrive F Z
    F: is not assigned.
    This should work as F: drive does exist.

    Script run with elevated cmd prompt.
      My Computer


  5. Posts : 777
    Windows 7
       #5

    Is F: drive a normal disk, or a DVD or removable drive? On that PC, can you copy the entire output from:
    powershell "Get-Partition"

    I need to see what's been reported locally. Thanks.
      My Computer


  6. Posts : 52
    Windows 10
    Thread Starter
       #6

    garlin said:
    Is F: drive a normal disk, or a DVD or removable drive? On that PC, can you copy the entire output from:
    powershell "Get-Partition"
    I need to see what's been reported locally. Thanks.
    On this PC, there are two opticals, 4 NVMe SSDs and 1 spinner. Drive I: is an external drive.

    Get-Partition returns nothing.

    Get-Partition -DriveLetter G returns an error;
    Code:
    get-partition : No MSFT_Partition objects found with property 'DriveLetter' equal to 'G'.  Verify the value of the
    property and retry.
    At line:1 char:1
    + get-partition -driveletter G:
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (G:Char) [Get-Partition], CimJobException
        + FullyQualifiedErrorId : CmdletizationQuery_NotFound_DriveLetter,Get-Partition
    My disk allocation;
    Change drive letters from command line via a script-diskmgmt.png
      My Computer


  7. Posts : 777
    Windows 7
       #7

    "Get-Partition -DriveLetter G" <-no colon

    That's very strange that Get-Partition returns no output. What about Get-Volume?
      My Computer


  8. Posts : 52
    Windows 10
    Thread Starter
       #8

    garlin said:
    "Get-Partition -DriveLetter G" <-no colon
    That's very strange that Get-Partition returns no output. What about Get-Volume?
    None of the storage applets work.
    Even this doesn't return anything: gcim MSFT_Disk -Namespace root/Microsoft/Windows/Storage
    Not that it relevant, but the storage service is running.
    This appears to be a known issue:

    Reddit - Dive into anything
    Reddit - Dive into anything

    Nothing in the event log.
      My Computer


  9. Posts : 777
    Windows 7
       #9

    Tom's Hardware sez:
    sc config winmgmt start= disabled
    net stop winmgmt
    Winmgmt /salvagerepository %windir%\System32\wbem
    Winmgmt /resetrepository %windir%\
    sc config winmgmt start= auto
      My Computer


  10. Posts : 52
    Windows 10
    Thread Starter
       #10

    That looks scary :)
    I think I will do that on my spare PC, not my main PC.
    Will get back to you.

    from https://techcommunity.microsoft.com/...ry/ba-p/373846

    If you suspect WMI or repository corruption, rebuilding repository is the last thing you should do. Deleting and rebuilding the repository can cause damage to the system or to installed applications. Other steps should be taken first to eliminate other possibilities or to confirm we have repository corruption.
      My Computer


 

  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 15:52.
Find Us




Windows 10 Forums