ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Change Drive Letter with PowerShell

    IT Discussion
    windows powershell
    4
    6
    3.5k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • scottalanmillerS
      scottalanmiller
      last edited by scottalanmiller

      Had an issue come up with a SAN tonight that mapped to the wrong drive letter and kept a server from coming up. Lots of fun. But it turned out that all I had to do was fixed the drive mapping. No GUI, though, so doing it from the command line was necessary. Here's how to do it.

      First we can look at our disk devices with Get-Disk, but this just helps us understand what devices we have and if they are healthy. We can't see what drive letters or filesystems are associated with the devices from here. Think of this as being like lsblk on Linux.

      > get-disk
      
      Number Friendly Name Serial Number                    HealthStatus         OperationalStatus      Total Size Partition
                                                                                                                   Style
      ------ ------------- -------------                    ------------         -----------------      ---------- ----------
      0      DELL PERC ... 008f58041e8dbf4d1900996254c0110b Healthy              Online                  465.25 GB GPT
      1      DELL PERC ... 0006f12d0a4d56cc2000996254c0110b Healthy              Online                    3.64 TB GPT
      2      WD My Book... WCC4EJLMCL37                     Healthy              Online                    3.64 TB GPT
      

      To see our file systems is gdr -PSProvider 'Filesystem', this is like the df command on Linux.

      > gdr -PSProvider 'Filesystem'
      
      Name           Used (GB)     Free (GB) Provider      Root                                               CurrentLocation
      ----           ---------     --------- --------      ----                                               ---------------
      C                  98.47        366.22 FileSystem    C:\                                                 Users\admin
      D                3240.11        484.76 FileSystem    D:\
      F                2147.86       1577.99 FileSystem    F:\
      G                                      FileSystem    G:\
      

      In my example above, we want to move the F:\ drive to be the E:\ drive. Here is how we do it.

      $drv = Get-WmiObject win32_volume -filter 'DriveLetter = "F:"'
      $drv.DriveLetter = "E:"
      $drv.Put() | out-null
      

      That's it. Not a one liner, but quite easy. PowerShell to the rescue. Run your gdr command again to see the change.

      1 Reply Last reply Reply Quote 2
      • JaredBuschJ
        JaredBusch
        last edited by

        @scottalanmiller Why not just get-psdrive?

        PS C:\WINDOWS\system32> Get-PSDrive
        
        Name           Used (GB)     Free (GB) Provider      Root                                               CurrentLocation
        ----           ---------     --------- --------      ----                                               ---------------
        Alias                                  Alias
        C                  55.11         24.35 FileSystem    C:\                                               WINDOWS\system32
        Cert                                   Certificate   \
        D                                      FileSystem    D:\
        E                 971.83        928.05 FileSystem    E:\
        Env                                    Environment
        Function                               Function
        HKCU                                   Registry      HKEY_CURRENT_USER
        HKLM                                   Registry      HKEY_LOCAL_MACHINE
        Variable                               Variable
        WSMan                                  WSMan
        
        1 Reply Last reply Reply Quote 3
        • dbeatoD
          dbeato
          last edited by

          A WD For backups?

          scottalanmillerS 1 Reply Last reply Reply Quote 0
          • scottalanmillerS
            scottalanmiller @dbeato
            last edited by

            @dbeato said in Change Drive Letter with PowerShell:

            A WD For backups?

            Seems that way.

            1 Reply Last reply Reply Quote 0
            • computerchipC
              computerchip
              last edited by

              What about using:

              Get-Partition -DriveLetter F | Set-Partition -NewDriveLetter E

              1 Reply Last reply Reply Quote 3
              • scottalanmillerS
                scottalanmiller
                last edited by

                @denis_mcgee said in Change Drive Letter with PowerShell:

                Very convenient, as for me

                Thanks

                1 Reply Last reply Reply Quote 0
                • 1 / 1
                • First post
                  Last post