script to change home drive RRS feed

  • Question

  • Hi,

    So I have home drive on DC1. I would like it to be remapped to home drive on DC2 upon boot. Either bat or ps1 example would be good. thanks.


    • Moved by Bill_Stewart Friday, March 9, 2018 7:35 PM This is not "teach me basics step-by-step" forum
    Thursday, October 12, 2017 5:05 PM

All replies

  • We don't write scripts for you. Give something a shot and we'll assist you in corrections as needed.

    To get you started, try using the Remove-PSDrive and New-PSDrive cmdlets in Powershell:


    or a simple bat file with "net use"


    We're happy to assist, but we won't do the work for you.

    Thursday, October 12, 2017 5:23 PM
  • I actually have a script which worked for removing shared drives assigned to any letter (which would also be a good option):

    $mapped = Get-WmiObject Win32_MappedLogicalDisk | Select DeviceId, ProviderName
    foreach ($item in $mapped) {
        if ($item.ProviderName -eq '\\DC1\share') {net use $item.DeviceId /del}

    but for whatever reason when I swap \\DC1\share with \\DC1\users\%username% it does not do anything. Any idea? After the old mapping is removed I could add a new link to a share with a simple net use.


    Thursday, October 12, 2017 5:41 PM
  • for a net use I tried:

    rem net use * \\DC1\users\%username% /delete /yes

    but it returns an error saying that 'conflicting switches detected'


    Thursday, October 12, 2017 5:45 PM
  • If your network drives are mapped based on the settings on their Active Directory account, you can update those settings programmatically using Powershell.

    Using the Get-ADUser and Set-ADUser cmdlets, you can read the location of their current Home directory, and update the location with the HomeDirectory parameter. All you'd need is add an operation to replace 'DC1' with 'DC2'

    Thursday, October 12, 2017 5:48 PM
  • You were on the right track here, just remove the "rem". So:

    net use <DriveName> /delete:yes

    where <DriveName> is the letter name of the drive.

    In regards to your other question, when you swap \\DC1\share with \\DC1\users\%username%. That's because you're using PowerShell there. Replace "%username%" with "$ENV:UserName" and try again.

    Thursday, October 12, 2017 5:52 PM
  • The home drive is set by AD.  Managing this is done via ADUC. Just change the AD user account settings to the mapping you want.


    • Edited by jrv Thursday, October 12, 2017 9:44 PM
    Thursday, October 12, 2017 9:43 PM
  • Thanks,

    Well I tried the following on BAT file but both return 'The syntax if this command is: (basically it's used incorrect):

    net use *: \\DC1\users\%username% /delete:yes
    net use * \\DC1\users\%username% /delete:yes

    I tried the following on PS1 as well (did not give any error but home folder was not removed either):

    1) Just variable

    $mapped = Get-WmiObject Win32_MappedLogicalDisk | Select DeviceId, ProviderName
    foreach ($item in $mapped) {
        if ($item.ProviderName -eq '$env:username') {net use $item.DeviceId /del}

    2) full path

    $mapped = Get-WmiObject Win32_MappedLogicalDisk | Select DeviceId, ProviderName
    foreach ($item in $mapped) {
        if ($item.ProviderName -eq '\\DC1\users\$env:username') {net use $item.DeviceId /del}

    any thoughts? btw, I did try to remove just \\dc1\share and it did work, so ps script works in general.


    • Edited by waer01 Friday, October 13, 2017 9:15 AM
    Friday, October 13, 2017 9:14 AM
  • As I noted.  The drive must be changed in AD.  If you try to change it AD will just block you and reset the configured drive.


    Friday, October 13, 2017 9:19 AM
  • well that means I will need to change 2000 profile entries in AD manually. the other issue is that I cannot change it for everyone, since we have a quite a few AD's in the environment. I just need to change it for exactly DC1 and users mapped under this DC1.


    Are you saying it's not possible to remap home drive via powershell script? or at least remove it like with a share example and just map a new drive via BAT?
    • Edited by waer01 Friday, October 13, 2017 9:27 AM
    Friday, October 13, 2017 9:25 AM
  • If you change one DC they all change.  There is only one AD per domain and all DCs are synchronized.

    You can change all profiles in ADUC by just selecting all users and changing the profile path once:'


    You should never store profiles on a DC.  It is a security and stability risk.


    Friday, October 13, 2017 9:29 AM
  • Sorry I am not sure what you are talking about. I have accounts under specific OU in AD. Each account has their home drive mapped to some letter under 'Properties->Profile->Home folder'. I cannot change all accounts drive letters under that specific OU. Since not all of them have to be changed as some accounts have their home drives mapped to DC3 for example. I need specifically to re-map home drives from \\DC1\\users\%homedrive% to \\DC2\users\%homedrive%.

    I do understand that I can go and change path in AD for each user manually. But that is not something I am looking for. 



    • Edited by waer01 Friday, October 13, 2017 9:52 AM
    Friday, October 13, 2017 9:50 AM
  • I recommend that you find someone in your company who is trained in AD and Windows networking.  You can also contact a consultant to work with you on this.  From your question and response it is clear you are not certified or trained in networking or AD. 

    There are numerous scripts in the Gallery that can change the home drive path in AD.  You can use one of those.

    AD is designed to manage home drives.  PowerShell has CmdLets that can manage home drives.

    Set-AdUser userid -HomeDrive X: -HomeDirectory \\server\shhare\userid

    See: https://technet.microsoft.com/en-us/library/ee617215.aspx

    In a domain this is managed by AD.   Users are not allowed to change this once it is set. 

    From an Admin account you can remove the drive and recreate it but AD will just reset it the next time GP refreshes in either 15 or 30 minutes


    • Proposed as answer by Jacorbello Monday, November 27, 2017 9:41 PM
    Friday, October 13, 2017 9:59 AM
  • OK, thanks for explaining. Now it makes sense. I have modified a bit my previous ps script and now it removes home drive:

    #Remap drives
    $mapped = Get-WmiObject Win32_MappedLogicalDisk | Select DeviceId, ProviderName
    foreach ($item in $mapped) {
        if ($item.ProviderName -eq "\\DC1\Users\$ENV:UserName") {net use $item.DeviceId /del}

    But as you said the change in AD under user Profile is still there.. I have tried to add the following line into PS as per your example:

    Set-AdUser userid -HomeDrive H: -HomeDirectory \\DC2\users\userid

    but unfortunately it returned an error: "The term 'Set-AdUser' is not recognized as the name of cmdlet, function, script file or operable program...."I need to dig more into the link you sent me and maybe I will be able to finalize it.


    Friday, October 13, 2017 10:35 AM
  • Again.  You need to learn about AD and how to use PowerShell.  The AD CmdLets only work on the AD server or on a workstation with RSAT installed. They cannot be used in a logon scripts or rom a workstation.

    You cannot makes these changes from a logon script. Only an admin can make these changes.

    Take some time and get a book on AD to learn how it works.  You should not mess with it until you are trained in AD. 


    Friday, October 13, 2017 10:43 AM
  • I had no issues using PS script inside logon script etc by remapping printers, shares, etc, worked no problem. I have no time to read a whole book right now or set-up a home lab environment to test, that why I am in this forum to ask for advice?

    I am testing on a test account with a test logon script, so no harm to AD either, before I figure out the final working method.


    Friday, October 13, 2017 10:54 AM
  • Yes you can map drives and printer just not drives  that are managed by AD.

    You need to take the time to learn or contact a consultant to help you understand this.  This is not a free consulting forum.  The form is for technicians who are trained in the technologies they are trying to use.

    As I noted above, the Gallery has scripts that can alter the AD home drive in bulk.


    Friday, October 13, 2017 11:16 AM