Link to home
Start Free TrialLog in
Avatar of SAM IT
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"

Open in new window

Avatar of Rich Weissler
Rich Weissler

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?
Avatar of SAM IT

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.
Avatar of SAM IT

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"

Open in new window

OK, so try this:

Import-Module ActiveDirectory
Get-Content "C:\Users\Administrator\Desktop\\input1.txt" |%{ Get-ADUser $_ | Move-ADObject -TargetPath "OU=disabledounew,OU,DC=test,DC=com"}

Open in new window


If that doesn't work, please post any errors you get.
Avatar of SAM IT

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\Desktop\new requriment\input.csv" |%{ Get-ADUser <<<<  $_ | Move-ADObject -TargetPath "OU=disabledounew,DC=test,DC=com"}
    + CategoryInfo          : ObjectNotFound: (samaccountname:ADUser) [Get-ADUser], ADIdentityNotFoundException
    + FullyQualifiedErrorId : Cannot find an object with identity: 'samaccountname' under: 'DC=test,DC=com'.,Microsoft.ActiveDirectory.Management.Commands.GetADUser
Avatar of SAM IT

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\Desktop\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.Management.ADObject' required by parameter 'Identity'. Specified method is not supported.
At line:2 char:94
+ $users = Get-ADUser "C:\Users\Administrator\Desktop\new requriment\input.csv" | Move-ADObject <<<<  $users -TargetPath "OU=disabledounew,DC=test,DC=com"
    + CategoryInfo          : InvalidArgument: (:) [Move-ADObject], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.ActiveDirectory.Management.Commands.MoveADObject
Is the file a CSV? Looks like you have a header on it. Did all the accounts move except for "samaccountname"?
Avatar of SAM IT

ASKER

its a csv file...looks like below one

samaccountname
test1
test2

only 2 account am trying to move to OU
ASKER CERTIFIED SOLUTION
Avatar of Jeremy Weisinger
Jeremy Weisinger

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SAM IT

ASKER

perfect...working fine

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.
Avatar of SAM IT

ASKER

Nicely  designed  script and Good  explanation
Avatar of SAM IT

ASKER

Good support . Thanks Jeremy
Glad to help. :)