none
Change drive mapping using Powershell? RRS feed

  • Question

  • Is there a way in Powershell to change a drive mapping from one location to another.  Example below:

    Drive M: is mapped to \\Server1\test_drive share

    That share has moved to another server with the same share name and now I want to have the following

    Drive M: now mapped to \\Server2\test_drive share

    As I will not know what drive letter each user is using mapping to what share,  I am basically trying to see if the user has a drive (any drive letter) mapped to Server1 and change that to Server2 (assuming the same share using same share name is on Server1 and Server2).

    I cannot wrap my head around this.

    Thanks,

    Dave


    Dave

    Friday, January 16, 2015 3:30 PM

Answers

All replies

  • Hi Dave,

    Why aren't you using Group Policy Preferences for this?


    Don't retire TechNet! - (Don't give up yet - 13,085+ strong and growing)

    • Proposed as answer by jrv Friday, January 16, 2015 5:09 PM
    • Marked as answer by Bill_StewartModerator Monday, May 18, 2015 5:09 PM
    Friday, January 16, 2015 3:33 PM
  • These users are scattered and not that many.  Maybe 200-500 out of 50K.  Seems easier to script and push it out a few at a time.

    Dave


    Dave

    Friday, January 16, 2015 7:05 PM
  • These users are scattered and not that many.  Maybe 200-500 out of 50K.  Seems easier to script and push it out a few at a time.

    Dave


    Dave

    That is old DOS sneakernet thinking.  These things have been done with GP since WS2003R2- 10 years since.


    ¯\_(ツ)_/¯


    • Edited by jrv Friday, January 16, 2015 7:12 PM
    Friday, January 16, 2015 7:11 PM
  • Just in case

    this is all it takse to remap a drive

    NET USE X: /delete
    NET USE X: \\server\share /persistent:yes


    ¯\_(ツ)_/¯

    Friday, January 16, 2015 7:14 PM
  • Nice if you know the drive letter is X: all the time.  Yes I am aware it could be done in GPOs. Not what I am looking for.  The drive letter might be X: or R:.  I am looking to just change the Server name in the drive mapping. So X: or R: that mapped to \\Server1\share1 will now map to \\Server 2\share2.

    The beauty of using a Powershell or other script is I can give it to someone who might not have access to GPOs and be able to push it to a couple of users and accommodate their needs by just changing a couple of variables each time. 

    Sorry for being so "Old School". 

    Dave


    Dave

    Friday, January 16, 2015 7:19 PM
  • Powershell:

    Set-PsDriveN
    et-PsDrive

    Simple.  It will take maybe lines.

    $drives=get-psdrive|?{$_.Root -match 'server1'}


    ¯\_(ツ)_/¯


    • Edited by jrv Friday, January 16, 2015 7:31 PM
    Friday, January 16, 2015 7:29 PM
  • I found the following script however it does not seem to be working.  But, I think it will do what I am trying to do.

    $OldServer = "\\EXAMPLE-FS-001\"
     $NewServer = "\\companyname.local\shares\"

     $drives = Get-WmiObject win32_logicaldisk |
        ? {$_.ProviderName -like "$($OldServer)*" } |
            % {
                $Name = (($_.DeviceID) -replace ":", "")
                $NewRoot = (($_.ProviderName) -replace $OldServer, $NewServer)
                Get-PSDrive $Name | Remove-PSDrive -Force -whatif
                New-PSDrive $Name -PSProvider FileSystem -Root $NewRoot -WhatIf
            }

    Dave


    Dave

    Friday, January 16, 2015 7:33 PM
  • You need to add -Persist to make it a mapped drive, otherwise it will be a temporary PSDrive only.

    http://technet.microsoft.com/en-us/library/hh849829.aspx

    99.9% sure you need a minimum of v3 for -Persist.


    Don't retire TechNet! - (Don't give up yet - 13,085+ strong and growing)

    Friday, January 16, 2015 7:34 PM
  • Cool, yep I see that the flag is missing.  Will give that a go.

    Thanks,

    Dave


    Dave

    Friday, January 16, 2015 7:36 PM