I'm creating a script to change certain OU users, but it gives a parameter error and it's not working.
Users and OUs are being listed through an .csv file, the strings get the information, but when putting it in the code line it doesn't work.
Import-Csv -Path C:\Users\leonan.martins\Desktop\teste1.csv -Delimiter ',' -PipelineVariable User | ForEach-Object -Process {
$us = $User.user
$OU1 = $User.Regiao
$OU2 = $User.Diretoria
$OU3 = $User.Area
get-aduser $User | move-adobject -TargetPath "OU=$OU1,OU=$OU2,OU=$OU3,DC=lab,DC=suporti,DC=local"
}
CodePudding user response:
The likable cause of your error is a typo on $us = $User.user
and the referencing $User
in the Get-ADUser
call instead of $us
. As for -PipelineVariable User
, you could replace it for $_
(also known as $PSItem
) to refer to the current object being passed through the pipeline:
Import-Csv -Path C:\Users\leonan.martins\Desktop\teste1.csv | ForEach-Object {
$target = [string]::Format(
"OU={0},OU={1},OU={2},DC=lab,DC=suporti,DC=local",
$_.Regiao, $_.Diretoria, $_.Area
)
Get-ADUser $_.User | Move-ADObject -TargetPath $target
}
CodePudding user response:
Add the DN of your target OU in your script, and you can do it as simple as this:
$Users = Import-Csv -Path "D:\Powershell\Input\UsersList.csv" #Path to CSV-File
$TargetOU = "OU=Target,OU=kj,DC=kj,DC=local" #DistinguishedName of Target OU
foreach($User in $Users)
{
Get-ADUser $User.SamAccountName | Move-ADObject -TargetPath $TargetOU
}