Need to get these results from powershell into a sql table.

Walter Cantin 1 Reputation point

$adgroups = Get-ADGroup -Filter "name -like 'DL'" | sort name

$data = foreach ($adgroup in $adgroups) {
$members = $adgroup | get-adgroupmember | sort name
foreach ($member in $members) {
Group = $
Members = $member.SamAccountName


Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,355 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 44,696 Reputation points

    The easiest way may be to create a CSV and import the CSV into the SQL table.

    Get-ADGroup -Filter "name -like 'DL'" | #Is the filter correct? Or should it be "DL*"?
        Sort-Object name |
                $groupname = $
                Get-ADGroupMember $_ | 
                    Sort-Object name |
                                Group = $groupname
                                Members = $_.SamAccountName
                        } | Export-Csv SomeFileName.csv -NoTypeInformation

    If you want to use PowerShell to import the CSV there are examples of how to do this if you search for "powershell import csv into sql".

    0 comments No comments

  2. Ian Xue (Shanghai Wicresoft Co., Ltd.) 29,486 Reputation points Microsoft Vendor


    You can use Invoke-Sqlcmd to execute Transact-SQL statements if you need to insert data into a sql table with a powershell script.
    For more details you can refer to this link

    Best Regards,


    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments