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 ""){
ForEach-Object
{
$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!