SAM IT
asked on
Unable to move multiple AD users to OU
Using below script I am to move single ADuser to OU, when i tried to move multiple users by adding get-content with variable its giving an error. Thanks in advance
Get-ADUser -Identity testuser| Move-ADObject -TargetPath "OU=disabledounew,DC=test,DC=com"
I'm not an expert in Powershell, so I'm not certain of the use of 'get-content' in this case. If you have Get-ADUser return multiple user objects, is there a reason you don't wrap the Move-ADObject in a For-EachObject?
ASKER
get-content is to import / capture the input file. get-aduser works only for single user move
Can you explain how you're using Get-Content?
Get-ADUser can get as many users as you want.
Get-ADUser can get as many users as you want.
ASKER
Please find the below . this is how am using get-content to import multiple users move it to different OU
Import-Module ActiveDirectory
$Users = Get-Content "C:\Users\Administrator\Desktop\\input1.txt" | Get-ADUser $users | Move-ADObject -TargetPath "OU=disabledounew,OU,DC=test,DC=com"
OK, so try this:
If that doesn't work, please post any errors you get.
Import-Module ActiveDirectory
Get-Content "C:\Users\Administrator\Desktop\\input1.txt" |%{ Get-ADUser $_ | Move-ADObject -TargetPath "OU=disabledounew,OU,DC=test,DC=com"}
If that doesn't work, please post any errors you get.
ASKER
getting below error
Get-ADUser : Cannot find an object with identity: 'samaccountname' under: 'DC=test,DC=com'.
At line:2 char:85
+ Get-Content "C:\Users\Administrator\De sktop\new requriment\input.csv" |%{ Get-ADUser <<<< $_ | Move-ADObject -TargetPath "OU=disabledounew,DC=test, DC=com"}
+ CategoryInfo : ObjectNotFound: (samaccountname:ADUser) [Get-ADUser], ADIdentityNotFoundExceptio n
+ FullyQualifiedErrorId : Cannot find an object with identity: 'samaccountname' under: 'DC=test,DC=com'.,Microsof t.ActiveDi rectory.Ma nagement.C ommands.Ge tADUser
Get-ADUser : Cannot find an object with identity: 'samaccountname' under: 'DC=test,DC=com'.
At line:2 char:85
+ Get-Content "C:\Users\Administrator\De
+ CategoryInfo : ObjectNotFound: (samaccountname:ADUser) [Get-ADUser], ADIdentityNotFoundExceptio
+ FullyQualifiedErrorId : Cannot find an object with identity: 'samaccountname' under: 'DC=test,DC=com'.,Microsof
ASKER
@rich ...I tried it as per your update . getting below error
PS C:\Users\Administrator> Import-Module ActiveDirectory
$users = Get-ADUser "C:\Users\Administrator\De sktop\new requriment\input.csv" | Move-ADObject $users -TargetPath "OU=disabledounew,DC=test, DC=com"
Move-ADObject : Cannot convert 'System.Object[]' to the type 'Microsoft.ActiveDirectory .Managemen t.ADObject ' required by parameter 'Identity'. Specified method is not supported.
At line:2 char:94
+ $users = Get-ADUser "C:\Users\Administrator\De sktop\new requriment\input.csv" | Move-ADObject <<<< $users -TargetPath "OU=disabledounew,DC=test, DC=com"
+ CategoryInfo : InvalidArgument: (:) [Move-ADObject], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Micr osoft.Acti veDirector y.Manageme nt.Command s.MoveADOb ject
PS C:\Users\Administrator> Import-Module ActiveDirectory
$users = Get-ADUser "C:\Users\Administrator\De
Move-ADObject : Cannot convert 'System.Object[]' to the type 'Microsoft.ActiveDirectory
At line:2 char:94
+ $users = Get-ADUser "C:\Users\Administrator\De
+ CategoryInfo : InvalidArgument: (:) [Move-ADObject], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Micr
Is the file a CSV? Looks like you have a header on it. Did all the accounts move except for "samaccountname"?
ASKER
its a csv file...looks like below one
samaccountname
test1
test2
only 2 account am trying to move to OU
samaccountname
test1
test2
only 2 account am trying to move to OU
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
perfect...working fine
can you please explain what does % and $_.
|%{ Get-ADUser $_.Samaccountname |
can you please explain what does % and $_.
|%{ Get-ADUser $_.Samaccountname |
Sure thing:
% is a alias for Foreach-Object cmdlet.
$_ is the object in the pipeline. In this case the object is a CSV row. The .samaccountname is the column that I want to get the value of.
% is a alias for Foreach-Object cmdlet.
$_ is the object in the pipeline. In this case the object is a CSV row. The .samaccountname is the column that I want to get the value of.
ASKER
Nicely designed script and Good explanation
ASKER
Good support . Thanks Jeremy
Glad to help. :)