Powershell search for AD accounts that end with a certain character

Hi EE .. anyone able to help with this?

I have a situation where i need to check a list of SamAccountNames and check if a corresponding "x" account exist in AD.

So on the list I want to enter a list of SamAccountnames


and I need the script to check if an account exist with those samAccountNames but that end with an "x"

Then have the "x" accounts found exported to a csv file.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Subash SundharanIT Infrastructure Architect Commented:
This is a simple code which get Samaccountname from the input file and check the AD for users with SamaccountnameX username. If it exists then it will send the user to report file. Hope this is what you are looking for.
GC C:\input.txt | %{Try{Get-ADUser "$($_)X" | Select -exp Samaccountname}Catch{}} | Out-File C:\report.txt

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
David Johnson, CD, MVPOwnerCommented:
Fleshed out version of the above
   Get All AD users that their SamAccountName ends with a specific Character
   Long description
  Get-ADAccountEndingWith C:\TEST\userlist.txt c:\test\output.txt X
    Get-ADAccountEndingWith -SourceFile C:\TEST\userlist.txt -OutputFile c:\test\output.txt -Search X
    This uses the defaults of 
    $SourceFile = "c:\test.txt",
    $OutputFile = "C:\temp\output.txt"
    $Search = "X"
function get-ADAccountEndingWith
        # Param1 help description
        #This is the source filename and path
        $SourceFile = "c:\test.txt",
        #Output Filename and path
        $OutputFile = "C:\temp\output.txt",
        #Search Character
        $Search = "X"


    Import-Module ActiveDirectory -ErrorAction Stop
    if((Test-Path $SourceFile)-eq $false){
        Write-Output "Input File Does Not Exist"
        Write-Verbose "Searching AD for users in "$sourcefile " for the Character:"$search
        $users = get-content $SourceFile
        foreach($user in $users)
            Get-ADUser "$($user)$searchcharacter" | 
            Select-Object -exp Samaccountname |Out-File $OutputFile -Append

Open in new window

MilesLoganAuthor Commented:
Thank you both for the great assistance .
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.