samaccountname for the given file of display names

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 6 years, 5 months ago.

  • Author
  • #6272

    by athreyatoo43 at 2013-02-11 06:03:25

    can someone help me with the script to get the samaccountname of a list of users by their display names.

    by coderaven at 2013-02-11 07:54:26

    I would suggest to use the ActiveDirectory module that comes with the RSAT tools

    Import-Module ActiveDirectory
    Get-Content -Path \\Path\to\File.txt | foreach-object { Get-ADUser -Identity $_ | Select Name, SamAccountName }

    by athreyatoo43 at 2013-02-12 03:52:43

    Does this ouput the file ? if so, where does it ? and what format.
    I want to give a excel or csv as input and want an excel sheet as output. please help

    by athreyatoo43 at 2013-02-12 04:17:23

    I Get the below error

    PS C:\Windows\system32> cd\
    PS C:\> cd users\ushasp\desktop
    PS C:\users\ushasp\desktop> Import-Module ActiveDirectory
    PS C:\users\ushasp\desktop> Get-Content -Path DIsplay-Names.txt
    Rajesh Bondu
    Nakul AR
    PS C:\users\ushasp\desktop> foreach-object { Get-ADUser -Identity $_ | Select Name, SamAccountName }
    Get-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Supply a non-null argument and try
    the command again.
    At line:1 char:38
    + foreach-object { Get-ADUser -Identity < <<< $_ | Select Name, SamAccountName }
    + CategoryInfo : InvalidData: (:) [Get-ADUser], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADUser

    by Klaas at 2013-02-12 05:03:07

    If you execute the Get-Content cmdlet alone, the result is written to the screen. After that you use the Foreach cmdlet but there is nothing in the pipe.
    You really have to use both cmdlets in combination, with the | in between, exactly as coderaven wrote.
    If you want to write this to a file, add | export-csv C] at the end.

    by athreyatoo43 at 2013-02-12 07:46:44

    HI Klaas, i am very bad at scripting.... could you please give me the complete script at once...

    by Klaas at 2013-02-12 08:22:57

    Import-Module ActiveDirectory
    Get-Content -Path C:\users\ushasp\desktop\DIsplay-Names.txt |
    foreach-object { Get-ADUser -Identity $_ | Select Name, SamAccountName }|
    export-csv C]

You must be logged in to reply to this topic.