Moving AD user to OU

Greetings, I have a scenario where a command works in my test environment, but not in production.
This is the line: $path = (Get-AdUser $modelafter).distinguishedName.Split(‘,’,2)[1]
In the test environment it copies the distinguished name minus the modelafter’s username and the next line moves to the target OU without issue.
In production, the same command copies the entire distinguished name of the modelafter and gives an error on the next line when trying to move to the target.
The execution policy for both is unrestricted for current user.
The error message is:
Move-ADObject : The operation could not be performed because the object’s parent is either uninstantiated or deleted
At C:\Users\patellin\Desktop\New folder\Add_AD_User.ps1:102 char:44

  • … DUser -Identity $TextBox_UName.Text | Move-ADObject -TargetPath $path
  •                                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : NotSpecified: (CN=Doe Jane,CN=Users,DC=bluelinx,DC=net:ADUser) [Move-ADObject], ADExcept

The version for the test environment:
PS C:\Windows\system32> $PSVersionTable

Name Value


PSVersion 5.1.17763.2931
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
BuildVersion 10.0.17763.2931
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1

Production:
PS C:\WINDOWS\system32> $PSVersionTable

Name Value


PSVersion 5.1.19041.1682
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
BuildVersion 10.0.19041.1682
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1

Thanks in advance

It is pretty likely that the $path you’re piecing together as your target path causes the issue. You may share all of the relevant code.

And BTW: When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

$TextBox_MA.Text) is the variable for model after
$TextBox_UName.Text is the variable for the username

$path = (Get-AdUser $TextBox_MA.Text).distinguishedName.Split(‘,’,2)[1]
Get-ADUser -Identity $TextBox_UName.Text | Move-ADObject -TargetPath $path

I figured out the issue. I’ll post the solution later.

$DN = (Get-AdUser $modelafter).distinguishedName
$User = [ADSI]“LDAP://$DN”
$path = $User.Parent -Replace “LDAP://”, “”
Get-ADUser -Identity $Username | Move-ADObject -TargetPath $path

Great. Thanks for sharing.

But again … When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink: