Powershell Move User to OU Cannot validate argument on parameter 'Identity

TheLoops 1 Reputation point

Good Morning/Afternoon,
I am having real trouble moving users to another OU using PowerShell ISE using 5.1.14393 on Server 2016.
Whatever I run I get: Move-ADObject : Cannot validate argument on parameter 'Identity'. The argument is null.

As I understand it, Move-ADObject needs a GUID or a DN. It seems to look at the DN property of the SamAccountName when i run as a CMDLET.

i have tried the source as a CSV file with just the samaccountnames listed with a SamAccountName header.
or just a text file with all the SamAccountNames listed. Even a CSV file with the DN's listed.

In this example the user samaccountname(s) are listed in users.txt without a header:

$Users = Get-Content -Path "C:\Temp\Users.txt"
ForEach ($user in $Users)
Move-ADObject -Identity $user -TargetPath "OU=Disabled,OU=Company Third Parties,DC=Company,DC=Company,DC=com"
Set-ADUser $user -Description "$(get-date) Disabled by IT Team"
write-host "Disabled User $User"

Import-Csv "c:\temp\users.csv"
ForEach-object {
Move-ADObject -Identity $($user.distinguishedname) -TargetPath "OU=Disabled,OU=Company Third Parties,DC=Company,DC=Company,DC=com" -WhatIf

Import-Csv -Path C:\temp\samaccountname.csv | ForEach-Object {
# Retrieve DN of User.
$UserDN = (Get-ADUser -Identity $_.Name).distinguishedName

# Move user to target OU.  
Move-ADObject -Identity $UserDN -TargetPath $TargetOU  


Import-Csv -Path C:\temp\samaccountname.csv
if ($User.'SamAccountName' -ne ""){
$UserDN = (Get-ADUser -Identity $user.Name).distinguishedName
Move-ADObject -Identity $UserDN -TargetPath $TargetOU -WhatIf

If i run a single cmdlet targetting the SamAcconutName, it works ok:
get-aduser Test.User | Move-ADObject -TargetPath "OU=Disabled,OU=Company Third Parties,DC=Company,DC=Company,DC=com" -WhatIf

I just can't get it to work in a foreach or foreach-object loop and ive burnt about 6-8 hours on this!!
Any help really really appreciated!! 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,416 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 45,186 Reputation points

    You cannot use the user object as an identity. A user object contains properties that may be used as an identity, though.

    Here's one way to do what your last example is trying to do:

    Import-Csv -Path C:\temp\samaccountname.csv |  
            if ($_.SamAccountName -ne "") {  
                $acct = $_.SamAccountName.trim()  
                    $UserDN = (Get-ADUser -Identity $acct -ErrorAction STOP).distinguishedName  
                    Move-ADObject -Identity $UserDN -TargetPath $TargetOU -ErrorAction STOP -WhatIf  
                    Write-Host "User $acct couldn't be found, or moved"  
    0 comments No comments