With PowerShell how do I export the remote administrators from a list of desktops into a CSV file?

Hello Experts,

My goal is to loop through the sample.txt file where we'll find a list of desktops. Afterward I'd like to record the Administrators membership list from each remote desktop and export to the test.csv file a comprehensive appended list of remote Administrators on each system. Any suggestions?

Each time I run the code I receive this message:
"cmdlet ForEach-Object at command pipeline position 1:
Supply values for the following parameters:
Process [0]"

Set-StrictMode -Version Latest
Set-ExecutionPolicy RemoteSigned -Force

$GroupName = "Administrators"
$AllGroups = @()
$OutputFile = "test.csv"
$ComputerName = Get-Content c:\Temp\Scripts\sample.txt

ForEach-Object -Begin {Test-Connection -ComputerName $ComputerName -Buffersize 16 -Count 2 -TimeToLive 5 -Quiet}
       -Process {
       $objWMI = (Get-WmiObject -ComputerName $ComputerName -Query "SELECT * FROM Win32_GroupUser WHERE GroupComponent=`"Win32_Group.Domain='$ComputerName',Name='$GroupName'`"")
          If ($objWMI -ne $Null) {
            ForEach ($objItem In $objWMI) {
                $DomainName = $objItem.PartComponent.Split(",")[0].SubString($objItem.PartComponent.Split(",")[0].IndexOf("`"")).Replace("`"", "")
                $UserName = $objItem.PartComponent.Split(",")[1].Replace("Name=", "").Replace("`"", "")
                $arrUsers = [Ordered]@{
                $objUsers = New-Object PsObject -Property $arrUsers
                $AllGroups += $objUsers
{$AllGroups | Export-csv $OutputFile -append -NoTypeInformation}
Who is Participating?
Dan McFaddenSystems EngineerCommented:
Here is an article with a better method.  The script is done and ready to use.  It also avoids having to use WMI to any operations.

link:  https://4sysops.com/archives/create-a-list-of-local-administrators-with-powershell/

CuriousMAUserAuthor Commented:
Thank you, Dan.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.