Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

error to change user ad from OU to powershell

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"
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading