'I need to add information from a .csv file in this OR exchange code

I have this link that changes the AD user from OU through information coming from a .csv file.

In my IT structure, the information regiao and diretoria are the information that must also be contained in the Department field of the ad user.

How do I get the information and reuse it in the variables?

`Department = $região + ", " + $Diretoria`




Import-Csv -Path  C:\Users\leonan.martins\Desktop\teste2.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
    Write-Host "OUs dos usuários ALTERADAS!"
}


Solution 1:[1]

If I understand correctly, you're looking to move the AD Object but also, set it's Department attribute, in which case you could either first set the user's attribute and then move it to the target Organizational Unit or vice-versa. You can use -PassThru to return the object you are working with which allows you to complete both operations in a streamed pipeline:

$ErrorActionPreference = 'Stop'

Import-Csv -Path C:\Users\leonan.martins\Desktop\teste2.csv | ForEach-Object {
    try {
        $target = [string]::Format(
            "OU={0},OU={1},OU={2},DC=lab,DC=suporti,DC=local",
            $_.Regiao, $_.Diretoria, $_.Area
        
        )

        Set-ADUser -Identity $_.User -Department ('{0},{1}' -f $_.Regiao, $_.Diretoria) -PassThru |
            Move-ADObject -TargetPath $target

        Write-Host "OUs dos usuários ALTERADAS!"
    }
    catch {
        Write-Warning $_.Exception.Message
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1