OK, so I exported the following information from the production environmrnt by using -
$users = Get-ADUser -Filter {(name -like '*(admin)*') -And (enabled -eq 'true')} | Select-Object sAMAccountName, Surname, UserPrincipalName, distinguishedname, @{n='ParentContainer';e={$_.distinguishedname -replace '^.+?,(CN|OU.+)','$1'}} | export-csv
c:\temp\admins.csv
Which got me the export I need.
Could anyone tell me where I am going wrong with the following to move the users to the correct OU -
$csvPath = "C:\temp\admins.csv"
$MoveUsers = Import-Csv $csvPath | ForEach-Object {
# Specify target OU.
$TargetOU = $MoveUsers.ParentContainer
# Retrieve DN of User.
$UserDN = Get-ADUser -Identity $MoveUsers.distinguishedName
# Move user to target OU.
Move-ADObject -Identity $UserDN -TargetPath $TargetOU
}
This is the error I am receicing back -
Get-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for
the argument, and then try running the command again.
At C:\temp\bulkmoveOU.ps1:6 char:36
+ $UserDN = Get-ADUser -Identity $MoveUsers.distinguishedName
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-ADUser], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.Ge
tADUser
Move-ADObject : Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value
for the argument, and then try running the command again.
At C:\temp\bulkmoveOU.ps1:8 char:29
+ Move-ADObject -Identity $UserDN -TargetPath $TargetOU
+ ~~~~~~~
+ CategoryInfo : InvalidData: (:) [Move-ADObject], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.Mo