Change users in AD

João Victor 21 Reputation points
2021-03-17T14:32:41.097+00:00

Hi guys!

I'm having trouble writing a script in PowerShell. I need a script that reads a CSV with two columns, later I need to filter the field in the first column and apply a value that is inside the other column in the user attributes in AD.

I wrote this script below but it is not working:

Import-Module ActiveDirectory
$Users = "C:\Users\joao.victor\Documents\leiaute.csv"
Import-CSV $Users -Header UserPrincipalName,title | %
Get-ADUser -Filter 'Name -like "
"' -SearchBase 'OU=Objetos Arquivados,OU=BHZ,OU=MG,DC=academico,DC=educacao,DC=elv,DC=intranet' -Properties * | % {Set-ADUser $_ -add @{title=$_.title}}*

Can anybody help me?

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
6,182 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,449 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 45,671 Reputation points
    2021-03-17T21:21:26.347+00:00

    Okay, let's try this version. The AD filter has always been quirky. This one contains an additional bit of data validation:

    Import-Module ActiveDirectory   # this probably isn't necessary any longer
    $OU = 'OU=Objetos Arquivados,OU=BHZ,OU=MG,DC=academico,DC=educacao,DC=elv,DC=intranet'
    $Users = "C:\Users\joao.victor\Documents\leiaute.csv"
    Import-Csv $Users -Header UserPrincipalName, title | # "Header" is only necessary if the CSV doesn't have a header row
        ForEach-Object{
            if ($_.UserPrincipalName.trim().length -ge 3){
                $upn = $_.UserPrincipalName.trim()
                if ($u = Get-ADUser -Filter {UserPrincipalName -eq $upn} -SearchBase $OU){  # "UPN" is unique. Using "-like" isn't necessary
                    $u | Set-ADUser -Title $_.title 
                }
                else{
                    Write-Host "Could not locate user: " $upn
                }
            }
            else{
                Write-Host "The UserPrincipalName '$($_.UserPrincipalName)' is either empty or too short"
            }
        }
    

3 additional answers

Sort by: Most helpful
  1. Rich Matheisen 45,671 Reputation points
    2021-03-17T14:54:35.807+00:00

    Try this:

    Import-Module ActiveDirectory   # this probably isn't necessary any longer
    $OU = 'OU=Objetos Arquivados,OU=BHZ,OU=MG,DC=academico,DC=educacao,DC=elv,DC=intranet'
    $Users = "C:\Users\joao.victor\Documents\leiaute.csv"
    Import-Csv $Users -Header UserPrincipalName, title | # "Header" is only necessary if the CSV doesn't have a header row
        ForEach-Object{
            if ($u = Get-ADUser -Filter "UserPrincipalName -eq $($_.UserPrincipalName)" -SearchBase $OU){  # "UPN" is unique. Using "-like" isn't necessary
                $u | Set-ADUser -Title $_.title 
            }
            else{
                Write-Host "Could not locate user: " $_.UserPrincipalName
            }
        }
    

  2. Ian Xue (Shanghai Wicresoft Co., Ltd.) 33,941 Reputation points Microsoft Vendor
    2021-03-18T12:57:31.393+00:00

    Hi,

    Please check to see if this works.

    $Users = "C:\Users\joao.victor\Documents\leiaute.csv"  
    $OU = 'OU=Objetos Arquivados,OU=BHZ,OU=MG,DC=academico,DC=educacao,DC=elv,DC=intranet'  
    Import-CSV $Users -Header UserPrincipalName,title | ForEach-Object{  
        $UPN = $_.UserPrincipalName  
        Get-ADUser -Filter {UserPrincipalName -eq $UPN} -SearchBase $OU | Set-ADUser -Title $_.title  
    }  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  3. Rich Matheisen 45,671 Reputation points
    2021-03-18T15:00:09.657+00:00

    Are you working in a multi-domain Active Directory forest? Is there a Global Catalog server in your AD site?

    Try this code and see if the exception contains the name of the server that you should be using:

    Import-Module ActiveDirectory   # this probably isn't necessary any longer
    $OU = 'OU=Objetos Arquivados,OU=BHZ,OU=MG,DC=academico,DC=educacao,DC=elv,DC=intranet'
    $Users = "C:\Users\joao.victor\Documents\leiaute.csv"
    Import-Csv $Users -Header UserPrincipalName, title | # "Header" is only necessary if the CSV doesn't have a header row
        ForEach-Object{
            if ($_.UserPrincipalName.trim().length -ge 3){
                $upn = $_.UserPrincipalName.trim()
                Try{
                    $u = Get-ADUser -Filter {UserPrincipalName -eq $upn} -SearchBase $OU -ErrorAction STOP
                    if ($u){
                       $u | Set-ADUser -Title $_.title
                    }
                    else{
                        Write-Host "Could not locate user: " $upn
                    }
                }
                Catch{
                    Write-Host "Finding '$upn' caused an exception. See if the referral points to the correct server for the user."
                    Write-Host $_.Exception.Referral
                }
            }
            else{
                Write-Host "The UserPrincipalName '$($_.UserPrincipalName)' is either empty or too short"
            }
        }
    
    0 comments No comments