Move AD user by Powershell

Marc 631 Reputation points
2022-10-26T10:07:31.31+00:00

I have often move AD users from one OU to another OU.
I am trying to do that by powershell.
In doing that I have to inherit/replace the Meberof list and change the HomeDrive & ScriptPath.
Could you please give me some suggestions or a script to work on?
Thanks

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,389 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 45,096 Reputation points
    2022-10-27T01:48:43.8+00:00

    I don't know, and you don't say, what to do if there's not a populated HomeFolder property. My best guess would be something like this:

    $DefaultHomeFolderLocation = '\\disk1\homefolder'  
      
    $u = Get-ADUser -Id useracct -properties HomeDirectory  
    if ($u.HomeDirectory){  
        $newHomeFolderPath = "$(Split-Path $u.HomeDirectory -Parent)" + "\$userName"  
    }  
    else{  
        $newHomeFolderPath = $DefaultHomeFolderLocation + "\$userName"  
    }  
    
    0 comments No comments

7 additional answers

Sort by: Most helpful
  1. Marc 631 Reputation points
    2022-10-26T18:41:14.35+00:00

    Thanks @Rich Matheisen this worked very well ;)

    What about to change HomeDirectory & HomeDrive

    I have an HomeDirectory path like this... "\disk1\homefolder\user1"

    I have to check if the users in the destination OU have the HomeDirectory & HomeDrive and if yes replace the path.

    For example:

    "\disk1\homefolder\user1"

    I have to get the first part and change use1 with the moved user name "user2"

    "\disk1\homefolder\user2"

    0 comments No comments

  2. Andreas Baumgarten 97,486 Reputation points MVP
    2022-10-26T19:00:17.5+00:00

    Hi @Marc ,

    you can try this:

    $userName = "mmouse"  
    $oldHomeFolderPath = "\\disk1\homefolder\user1"  
    $newHomeFolderPath = "$(Split-Path $oldHomeFolderPath -Parent)" + "\$userName"  
    $newHomeFolderPath   
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

  3. Marc 631 Reputation points
    2022-10-26T22:48:32.64+00:00

    Hi @Andreas Baumgarten

    thank you it worked perfectly fine if in the destination user is present a path. In the case in the user property is not present a path then give error.

    The script needs an if to check if the destination OU users have the Home directory or not.

    I have used "-like" "-match" "-contains" but it didn't work

    What should I use to check if it is present a path?

    0 comments No comments