locked
Get-ADUser SamAccount from CSV file does not work. RRS feed

  • Question

  • So I'm trying to get HomeDirectory + SamAccountName using Get-ADUser from an imported csv file called $csvFile

    $GetUserInfo = Get-ADUser (Get-ADUser -Filter { displayName -eq $csvFile[$index].DisplayName}).SamAccountName -Properties SamAccountName,HomeDirectory

    It gives me an error. 

    Get-ADUser : Invalid type 'System.Object[]'.
    Parameter name: displayName
    At line:1 char:28
    + $GetInfoUser = Get-ADUser (Get-ADUser -Filter { displayName -eq $csvFile[5].Disp ...
    +                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ArgumentException
        + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.GetADUser


    However if I do like this 

    $DisplayName = $csvFile[$index].DisplayName

    and replace $DisplayName to the filter like this, it works flawless and I get HomeDirectory and the SamAccountName

    (Get-ADUser -Filter { displayName -eq $DisplayName }).SamA...


    Any ideas or improvements


    Thursday, August 21, 2014 10:14 AM

Answers

  • Still produces the same error. I'm running 4.0

    How about this?

    Import-Csv C:\Temp\user.csv | % {
        $displayname = $_.displayname; 
        get-aduser -filter { displayname -eq $displayname } | Select SamAccountname , HomeDirectory
    }

    The below was my CSV format

    DisplayName
    Chendrayan Venkatesan
    Ramesh Venkatesan

    Regards Chen V [MCTS SharePoint 2010]



    • Edited by Chen VMVP Thursday, August 21, 2014 2:05 PM
    • Marked as answer by AnnaWY Friday, August 29, 2014 7:23 AM
    Thursday, August 21, 2014 1:33 PM

All replies

  • what does your csv-file look like ?

    can you give me an example of your header and 1 row of data ?

    for example:

    samaccountname;displayname

    bgates; Gates, Bill

    ...


    Thursday, August 21, 2014 10:32 AM
  • "objectClass","DisplayName","SamAccountName","Description"

    "user","MyDisplayName","mydispname","myUserDescription"

    Thursday, August 21, 2014 10:50 AM
  • Try the following snippet instead. It will loop through every row in the CSV-file and output the users SamAccountName and HomeDirectory:

    $csvFile = Import-Csv PathToFile.csv
    
    # Output result in console
    Foreach ($csvRow in $csvFile)
    {
        Get-ADUser -Filter {displayName -eq $($csvRow.DisplayName)} -Properties SamAccountName,HomeDirectory | Select-Object SamAccountName, HomeDirectory
    }

    If you want to save the output to a variable and post-process the data you could save the output with the following snippet:

    # Save output into variable
    $UserData = Foreach ($csvRow in $csvFile)
    {
        Get-ADUser -Filter {displayName -eq $($csvRow.DisplayName)} -Properties SamAccountName,HomeDirectory | Select-Object SamAccountName, HomeDirectory
    }

    Thursday, August 21, 2014 11:11 AM
  • "objectClass","DisplayName","SamAccountName","Description"

    "user","MyDisplayName","mydispname","myUserDescription"

    Try this

    Import-Csv C:\Temp\User.csv |

    % {Get-ADUser -Identity $_.SamaccountName}

    To fetch required details

    Import-Csv C:\Temp\User.csv |

    % {Get-ADUser -Identity $_.SamaccountName | Select -Property Samaccountname , HomeDirectory}



    Regards Chen V [MCTS SharePoint 2010]


    • Edited by Chen VMVP Thursday, August 21, 2014 11:12 AM
    Thursday, August 21, 2014 11:11 AM
  • Still produces the same error. I'm running 4.0
    Thursday, August 21, 2014 11:32 AM
  • Still produces the same error. I'm running 4.0

    How about this?

    Import-Csv C:\Temp\user.csv | % {
        $displayname = $_.displayname; 
        get-aduser -filter { displayname -eq $displayname } | Select SamAccountname , HomeDirectory
    }

    The below was my CSV format

    DisplayName
    Chendrayan Venkatesan
    Ramesh Venkatesan

    Regards Chen V [MCTS SharePoint 2010]



    • Edited by Chen VMVP Thursday, August 21, 2014 2:05 PM
    • Marked as answer by AnnaWY Friday, August 29, 2014 7:23 AM
    Thursday, August 21, 2014 1:33 PM