Export-Csv results in System.Object[] when Get-ADUser finds multiple SamAccountNames matching RRS feed

  • Question

  • I have seen this addressed in a few forums, but haven't been able to figure out my issue.  I pulled this script from Technet.  Now if I run the script, it works, but also returns accounts that have been disabled.  I input the "Enabled -eq $True" filter, but it doesn't seem to be working.  I would be ok with it if when I add "Export-Csv path" to the end of the script it would return with ALL of the SAMAccountNames in the Csv, however it will only return "System.Object[]" for any with multiple matches.  Does anyone know how I can:

    a.) Correctly filter out disabled accounts

    b.) Make results with multiple SamAccountNames display correctly in csv

    Any and all help is appreciated.

    Here is the script:

    Import-Module C:\BHSRosterScript\GetADUserInfo.psm1


    -CsvFilePath C:\BHSRosterScript\NeedSAMAccountName2.csv

    And the function it calls on:


    #The sample scripts are not supported under any Microsoft standard support

    #program or service. The sample scripts are provided AS IS without warranty 

    #of any kind. Microsoft further disclaims all implied warranties including, 

    #without limitation, any implied warranties of merchantability or of fitness for

    #a particular purpose. The entire risk arising out of the use or performance of 

    #the sample scripts and documentation remains with you. In no event shall

    #Microsoft, its authors, or anyone else involved in the creation, production, or

    #delivery of the scripts be liable for any damages whatsoever (including,

    #without limitation, damages for loss of business profits, business interruption,

    #loss of business information, or other pecuniary loss) arising out of the use

    #of or inability to use the sample scripts or documentation, even if Microsoft

    #has been advised of the possibility of such damages


    #requires -Version 2.0








            Get-OSCSamAccountName is an advanced function which can be used to get active directory user SamAccount name.


            Get-OSCSamAccountName is an advanced function which can be used to get active directory user SamAccount name.

        .PARAMETER  CsvFilePath

    Specifies the path you want to import csv files.


            C:\PS> Get-OSCSamAccountName -CsvFilePath C:\Script\Users.csv

    This command will list all active directory user SamAccount Name info.
















    If(Test-Path -Path $CsvFilePath)



    #import the csv file and store in a variable


    $Names = (Import-Csv -Path $CsvFilePath).DisplayName



    Foreach($Name in $Names)



    # $Name = $Name.Replace(" ","") -split ","


    $Name = $Name -split ","


    $FirstName = $Name[0].Trim()


    $LastName = $Name[1].Trim()


    $UserName = $LastName + ", " + $FirstName


    $filter = "surname -eq '$lastanme' -and GivenName -eq '$firstName'"


    #Retrieve the ad users based on previous two variables.


    $SamAccountName = Get-ADUser -Filter{ surname -eq $LastName -and GivenName -eq $FirstName -and (Enabled -eq $True) } | select -ExpandProperty SamAccountName



    If($SamAccountName -eq $null)



    $SamAccountName = "NotFound"



    #Output the result


    New-Object -TypeName PSObject -Property @{DisplayName = $UserName


    = $SamAccountName









    Write-Warning "Cannot find path '$CsvFilePath' because it does not exist."




    Monday, April 13, 2015 6:49 PM


All replies