Change drive letters from command line via a script

Page 2 of 4 FirstFirst 1234 LastLast

  1. Posts : 52
    Windows 10
    Thread Starter
       #11

    Sorry for the delay in responding.
    Tried the above commands on a fresh Windows install. Get-Partition returns nothing prior to running the command and nothing after commands and a reboot.

    Code:
    Microsoft Windows [Version 10.0.19044.3570]
    (c) Microsoft Corporation. All rights reserved.
    
    C:\Windows\system32>sc config winmgmt start= disabled
    [SC] ChangeServiceConfig SUCCESS
    
    C:\Windows\system32>net stop winmgmt
    The following services are dependent on the Windows Management Instrumentation service.
    Stopping the Windows Management Instrumentation service will also stop these services.
    
       Intel(R) Dynamic Application Loader Host Interface Service
       IP Helper
    
    Do you want to continue this operation? (Y/N) [N]: y
    
    The Intel(R) Dynamic Application Loader Host Interface Service service was stopped successfully.
    
    The IP Helper service is stopping.
    The IP Helper service was stopped successfully.
    
    The Windows Management Instrumentation service is stopping.
    The Windows Management Instrumentation service was stopped successfully.
    
    
    C:\Windows\system32>Winmgmt /salvagerepository %windir%\System32\wbem
    WMI repository is consistent
    
    C:\Windows\system32>Winmgmt /resetrepository %windir%\
    WMI repository has been reset
    
    C:\Windows\system32>sc config winmgmt start= auto
    [SC] ChangeServiceConfig SUCCESS
    
    C:\Windows\system32>
    
    [Reboot]
    
    Windows PowerShell
    Copyright (C) Microsoft Corporation. All rights reserved.
    
    Try the new cross-platform PowerShell https://aka.ms/pscore6
    
    PS C:\Users\Joel>get-partition
    PS C:\Users\Joel>
      My Computer


  2. Posts : 776
    Windows 7
       #12

    Could this be a driver issue? Does this system use RAID mode (but not necessarily RAID)?
      My Computer


  3. Posts : 52
    Windows 10
    Thread Starter
       #13

    System uses AHCI mode.

    All drivers (chipset, audio, LAN) are current. I do not use wireless or BT even if motherboard supports it. All PCs are either 2.5G or 10G LAN. There are no exotic devices installed, just boot SSD and a spinner.

    Before doing above test I wiped system and reinstalled from scratch. I have not yet installed any applications on that PC.

    The PCs I tried have X299 and Z790 motherboards.

    I've just added powershell 7.4.0 and the storage cmds don't work in that version either
      My Computer


  4. Posts : 776
    Windows 7
       #14

    I feel filthy about piping commands thru diskpart...

    ChangeDrive.bat
    Code:
    <# : batch script
    @echo off
    powershell -nop Invoke-Expression ('$OldDrive = """"%1""""; $NewDrive = """"%2""""' + [System.IO.File]::ReadAllText('%~f0'))
    goto :eof
    #>
    
    $Volume = @{}
    
    foreach ($line in ("list vol" | diskpart | where { $_ -match 'Volume' })) {
        if ($line -match "Volume ([0-9]+)[\s]+([A-Z]) " -eq $true) {
            $Volume.Add($Matches[2], $Matches[1])
        }
    }
    
    if (-not ($Volume.ContainsKey($OldDrive))) {
        "{0}: is not assigned." -f $OldDrive
        exit
    }
    
    if ($Volume.ContainsKey($NewDrive)) {
        "{0}: is unavailable." -f $NewDrive
        exit
    }
    
    try {
        "select volume $($Volume[$OldDrive])`nassign letter=$($NewDrive)" | diskpart | Out-Null
        "Changed {0}: to {1}:" -f $OldDrive, $NewDrive
    }
    catch {
        $_.Exception.Message
        exit 1
    }
      My Computer


  5. Posts : 52
    Windows 10
    Thread Starter
       #15

    That seems to work. Need to do some more extensive testing though.

    I have a batch script I run on every PC which automates the configuration for each PC. This drive letter setup has been a PITA for years, thank you for your help.

    I imagine I would call this script repetitively from my main script.

    Code:
    call chgdrive J A
    call chgdrive I B
    call chgdrive H I 
    etc
    The main script writes output to a log file (I do not want to see results on screen), which is set as a variable %logfile%

    Code:
    chgdrive J A >>%logfile% 2>&1
    I imagine this would not work. How do I write the output from your script to the log file?

    Code:
    rshell -nop Invoke-Expression ('$OldDrive = """"%1""""; $NewDrive = """"%2"""" ; $logfile = """"%3""""' + [System.IO.File]::ReadAllText('%~f0'))
    But what goes after
    Code:
    "{0}: is not assigned." -f $OldDrive
    "{0}: is unavailable." -f $NewDrive
    "Changed {0}: to {1}:" -f $OldDrive, $NewDrive
    To append the results to the log file?
      My Computer


  6. Posts : 776
    Windows 7
       #16

    Why wouldn't it work?
    Code:
    C:\Users\GARLIN\Downloads>set logfile=log.txt
    
    C:\Users\GARLIN\Downloads>ChangeDrive.bat Z E >>%logfile% 2>&1
    
    C:\Users\GARLIN\Downloads>more log.txt
    Changed e: to z:
    Changed Z: to E:
      My Computer


  7. Posts : 52
    Windows 10
    Thread Starter
       #17

    I don't know.
    Anyway, you're right it works.

    And so does adding >>$logfile to your script.

    This is going to save me heaps of time. Thanks for your help.
      My Computer


  8. Posts : 52
    Windows 10
    Thread Starter
       #18

    A major flaw in my logic.
    As Windows assigns drive letters to optical and removable drives last, these are the drives that need to be changed.
    Problem is you can't run a script from a drive and change the drive letter. It causes the script to crash

    So, on this system my optical drive is assigned I: and my drive with my scripts is H:
    So when I run
    chgdrive I A - that's fine
    chgdrive H I - Kills the executing script because H: is no longer found.

    Another issue is that the script sets up lots of variable at the start

    set src=%cd:~0,2%
    set srcpath=%src%\Postinstall\%USERNAME%
    and many others

    These will be recorded as H: and H:\Postinstall\Joel as this is what the drive letter was at the start of the script.

    Any way around that?
      My Computer


  9. Posts : 6,345
    Windows 11 Pro - Windows 7 HP - Lubuntu
       #19

    You can do it with diskpart assign
    To do on a batch file create a diskpart script text file.
    https://learn.microsoft.com/en-us/wi...s-and-examples

    Example of a scriptname.txt (to change partition D: on drive 0 to T: )
    sel disk 0
    sel vol D
    assign letter=T

    Example of Rename.cmd
    diskpart /s scriptname.txt
      My Computers


  10. Posts : 16,952
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #20

    jshs2020 said:
    A major flaw in my logic.
    As Windows assigns drive letters to optical and removable drives last, these are the drives that need to be changed.
    Problem is you can't run a script from a drive and change the drive letter. It causes the script to crash
    Run an additional script first - one that copies the main script to a known place on the OS disk.
    Then run the script from the OS disk [the additional script can run it as its last action so it is not hanging around disrupting your drive letter changes].

    Personally, I put all scripts within a decently-protected folder created for the purpose on the OS disk.
    Set up my Tools folder ditty - TenForums


    Denis
      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 14:53.
Find Us




Windows 10 Forums