powershellactive-directorytext-filesmultiple-usersdisplayname-attribute

Searching Active Directory and exporting a list of names in mulitpule groups to a txt file for powershell


I am attempting to create a simple PowerShell script that will look at a list of Active Directory groups from a text file, then search Active Directory for the Groups (regardless of type), then get the list of members in each of the groups, and then output their "name" attribute of each user to an output text file. Now I pretty much have everything done except the last part. When it outputs the name to the text file, it outputs only ONE name to the file and not the other 300. However, if I take off the output function the script outputs all of the names like I want it to in the console only. Can someone explain why my method of getting this done is not working? I am really curious as to why I cannot direct the output to the file the way I want to. Additionally, the script loops through everything correctly and I know it finds the groups as well (I know this because when I look at the text files i see one entry for each file), but the script goes through the first 8 groups and the starts throwing errors stating that it cannot find the specific groups that it supposed to loop through. But it already found them and outputted only one entry to each of the files. Why is this?

I more interested in the answer for my first question because the script still does what its supposed to do correctly.

So, to reiterate, I would like to know why the script is only outputting one name to the file when it should be outputting 300+ for each group.

      ##This is variable that will hold the file path for the list of Active Directory Groups##

 $file='C:\Users\me\Desktop\DL_Names.txt';

 ##Command to dump the list into a variable##

 $DLnames=get-content $file;

 ##This is the variable to hold the path for where the output files are to be placed##

 [string]$path='C:\Users\me\Desktop\DL_repository\';

 ##Loop through the variable and for each entry preform the instruction listed##

 foreach ($name in $DLnames)
 {

 ##These two variables are used to create the file name for the output files##

 [string]$filename=$name+'.txt';

  [string]$Fullpath=$path+$filename;

 #This variable is used to determine if the groups exists in Active Directory##

 $verifygroupexists = Get-ADGroup -Identity $name;

 ##This is the if statement that is used to determine if the group exists in Active Directory##

 if($verifygroupexists -eq $null)

 {

 ##If the group doesnt exist, create a file and output the string to the file stating so with the group name##

 ##Still working on the removing portion of this, need help##

 New-Item $fullpath -ItemType file;

 [string]$error='AD Group'+' '+$name+' '+'does not exist';

 $error | Out-File -filepath $Fullpath;

 $Removeentry=$name;

 $name.Remove($Removeentry);

 }  

 else

 {

 ##If the group does exist in Active Directory then create a new text file to be used for output.

 New-Item $fullpath -ItemType file;

       ##Get the list of memebrs in the group and place them into a new variable##

 $groupmember=get-adgroupmember -Identity $name;

              ##Now loop through each entry in the new variable and output to the text file each member's 'name' (A.K.A. Displayname)##

 foreach ($user in $groupmember)

 {


              ##This is where my issue is, its not outputting all of the names to the text file##

             $displayname=get-aduser -identity $User.SamAccountName | select name | Out-File -filepath $Fullpath; 


              };



         };


 }; 

As an example the out put always looks like this:

name

Last, Name1

When it should be:

name

Last, Name1

Last, Name2

Last, Name3

Last, Name4

Last, Name5

etc, etc, etc


Solution

  • This line is called for each user in the group, and each time it runs, it overwrites the contents of the file.

    $displayname=get-aduser -identity $User.SamAccountName | select name | Out-File -filepath $Fullpath;
    

    If you replace Out-File with Add-Content, then the command will append to the file. If you're running the script repeatedly, make sure to use Set-Content to clear down the file first. You also don't need to set the value of a variable $displayname as part of this step:

    Set-Content -Value "" -Path $Fullpath
    get-aduser -identity $User.SamAccountName | select name | Add-Content -Path $Fullpath;