locked
PowerShell Get-ADuser from CSV RRS feed

  • Question

  • Hi All,

    I am struggling to get AD users (1000) from a CSV or text file and set some AD properties. After following many examples out on the internet already it seems like I need an appropriate filter to target the users in AD

    Requirements:

    I need to be able to import a CSV or Text file that has the Name Attribute of user "firstname lastname"

    I then need to use Set-AdUser -clear extensionAttribute15

    So as a standalone test i can complete the action: (using -whatif switch)

    Get-ADUser -identity "User" | Set-ADUser -clear extensionattribute15

    I've the tried to do this for a bulk of users a couple of ways, initially just using the "Get-Aduser" to ensure it can query this in AD

    Get-Content "C:\Temp\Attribute15.txt" | ForEach-Object { Get-ADUser -Identity $_ }

    The error is that the account cannot be found under the Domain

    After some troubleshooting I found that this would only accept the pre-2000 name, so testing with this I can see the action would work as expected

    Get-Content "C:\Temp\Attributex.txt" | ForEach-Object { Get-ADUser -Identity $_ | Set-ADUser -Clear extensionattributex -WhatIf}

    My question is how could i potentially use the existing text file with the Name "FirstName LastName" to do the task

    The original data was from a dump in AD connect which lists the users DistinguishedName so this would be an option it is possible to target this as the source. The AD attribute has a string that is too long for the cloud to accept so I would like to remove it and avoid doing this manually

    Any thoughts how this might be achieved?

    Kind Regards

    Dean



    Friday, April 12, 2019 3:57 PM

Answers

  • Thanks for the reply, here are a few things I managed to find to assist with the issue

    $results = @()
    
    $objects= Get-Content "C:\Temp\DN-Values.txt"
    
    ForEach($object in $objects)
    
        {
    
         $results += Get-ADObject -Server "GBVS03ISDC01:3268" -Filter {distinguishedName -eq $object} -property cn, SamAccountName | Select-Object  cn,  @{N='Domain\username';E={ "$((($_.DistinguishedName -split 'DC=')[1]).Replace(',',''))\$($_.SamAccountName)"}}
    
        }
    
    $results| export-csv "C:\Temp\Wanted-info.csv" -NoTypeInformation


    The code above created a new CSV with "Domain\Username"

    I then used a simple Excel Trim to remove the "Domain\" to get just the Samaccountname

    =RIGHT(A2,LEN(A2)-4

    The CSV is now in a working format I can use

    Thanks


    • Marked as answer by exchinfo Monday, April 15, 2019 8:03 AM
    Monday, April 15, 2019 8:03 AM

All replies

  • Users cannot be identified uniquely by first and last name, so you cannot use the -Filter parameter. If "firstname lastname" in the txt file is meant to match the Relative Distinguished Name (RDN, or the value of the cn attribute) then you need to use -Filter parameter, similar to:

    Get-ADUser -Filter {Name -eq $_}
    In this case there could be no match, one match, or more than one, since Name is only guaranteed to be unique in the parent OU or container. Unless you know for sure that Name is unique in the domain, your code should deal with all three possibilities, and only update if there is but one user found.


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Friday, April 12, 2019 4:37 PM
  • Thanks for the reply, here are a few things I managed to find to assist with the issue

    $results = @()
    
    $objects= Get-Content "C:\Temp\DN-Values.txt"
    
    ForEach($object in $objects)
    
        {
    
         $results += Get-ADObject -Server "GBVS03ISDC01:3268" -Filter {distinguishedName -eq $object} -property cn, SamAccountName | Select-Object  cn,  @{N='Domain\username';E={ "$((($_.DistinguishedName -split 'DC=')[1]).Replace(',',''))\$($_.SamAccountName)"}}
    
        }
    
    $results| export-csv "C:\Temp\Wanted-info.csv" -NoTypeInformation


    The code above created a new CSV with "Domain\Username"

    I then used a simple Excel Trim to remove the "Domain\" to get just the Samaccountname

    =RIGHT(A2,LEN(A2)-4

    The CSV is now in a working format I can use

    Thanks


    • Marked as answer by exchinfo Monday, April 15, 2019 8:03 AM
    Monday, April 15, 2019 8:03 AM
  • I think this is what you are trying to do:

    $properties = @( 'cn', 'SamAccountName', @{ n = 'Domain\username'; e = { (($_.DistinguishedName -split 'DC=')[1] -replace ',') + '\' + $_.SamAccountName } } )

    #' Get-Content C:\Temp\DN-Values.txt | ForEach-Object{ Get-AdUser $_ -Server 'GBVS03ISDC01:3268' } | Select-Object $properties | export-csv C:\Temp\Wanted-info.csv -NoTypeInformation

    You do not need to filter when using a DistinguishedName as it is a known identifier.  Also we would us Get-AdUser and not Get-AdObject.

    In this case we would gain by using the pipeline to do this.


    \_(ツ)_/




    • Edited by jrv Monday, April 15, 2019 8:21 AM
    Monday, April 15, 2019 8:20 AM