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

USer1
User2
USer3

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

Then have the "x" accounts found exported to a csv file.
LVL 2
MilesLoganAsked:
Who is Participating?
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.

SubsunCommented:
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

0

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
<#
.Synopsis
   Get All AD users that their SamAccountName ends with a specific Character
.DESCRIPTION
   Long description
.EXAMPLE
  Get-ADAccountEndingWith C:\TEST\userlist.txt c:\test\output.txt X
.EXAMPLE
    Get-ADAccountEndingWith -SourceFile C:\TEST\userlist.txt -OutputFile c:\test\output.txt -Search X
.EXAMPLE
    Get-ADAccountEndingWith 
    This uses the defaults of 
    $SourceFile = "c:\test.txt",
    $OutputFile = "C:\temp\output.txt"
    $Search = "X"
#>
function get-ADAccountEndingWith
{
    [CmdletBinding()]
    [OutputType([int])]
    Param
    (
        # Param1 help description
        [Parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        #This is the source filename and path
        $SourceFile = "c:\test.txt",
        #Output Filename and path
        $OutputFile = "C:\temp\output.txt",
        #Search Character
        $Search = "X"

    )

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

Open in new window

1
MilesLoganAuthor Commented:
Thank you both for the great assistance .
0
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
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.