function get-localadmins{
  [cmdletbinding()]
  Param(
  [string]$computerName
  )
  $group = get-wmiobject win32_group -ComputerName $computerName -Filter "LocalAccount=True AND SID='S-1-5-32-544'"
  $query = "GroupComponent = `"Win32_Group.Domain='$($group.domain)'`,Name='$($group.name)'`""
  $list = Get-WmiObject win32_groupuser -ComputerName $computerName -Filter $query
  $list | ForEach{$_.PartComponent} | ForEach{$_.substring($_.lastindexof("Domain=") + 7).replace("`",Name=`"","\")}
}
foreach($computer in (Get-ADComputer -filter * | select name).name){
Write-Verbose "Checking $computer" -Verbose
get-localadmins -computerName $computer
}

Hello everyone,

I am having a hard time with this one. I am using this script to pull a list of local admins on all domain joined machines. The script works as intended and inputs the results on the console (this is the only script I have found online that works so far). I was wondering how to output the results onto a CSV. I keep running into errors when using the export-csv command. Any help would be greatly appreciated.

3 Spice ups

how about like so?

function get-localadmins{
  [cmdletbinding()]
  Param(
  [string]$computerName
  )
  $group = get-wmiobject win32_group -ComputerName $computerName -Filter "LocalAccount=True AND SID='S-1-5-32-544'"
  $query = "GroupComponent = `"Win32_Group.Domain='$($group.domain)'`,Name='$($group.name)'`""
  $list = Get-WmiObject win32_groupuser -ComputerName $computerName -Filter $query
  $list | ForEach{$_.PartComponent} | ForEach{$_.substring($_.lastindexof("Domain=") + 7).replace("`",Name=`"","\")}
}
foreach($computer in (Get-ADComputer -filter * | select name).name){
Write-Verbose "Checking $computer" -Verbose
get-localadmins -computerName $computer |
export-csv "report.csv" -notypeinformation -append
}

HI @alexw ​! thank you for the assistance. Currently running the updated script. Once the script finishes I will follow up with the results.

Looks like the script finished but no output file was generated.

Does running the plain function create output?

get-localadmins -computerName $computer

Sorry pretty new to pwershell. If you mean literally just running the line below then it generates The term “get-localadmins” is not recognized as the name of a cmdlet.

get-localadmins -computerName $computer

well if you load the function first of course…

this seem to work fine for me,

function get-localadmins {
    [cmdletbinding()]
    Param(
        [string]$computerName
    )
    if (test-connection $computer -Count 1 -Quiet) {
        $group = get-wmiobject win32_group -ComputerName $computerName -Filter "LocalAccount=True AND SID='S-1-5-32-544'"
        $query = "GroupComponent = `"Win32_Group.Domain='$($group.domain)'`,Name='$($group.name)'`""
        $list = Get-WmiObject win32_groupuser -ComputerName $computerName -Filter $query
        $list | ForEach { $_.PartComponent } | ForEach {
            [pscustomobject]@{
                ComputerName = $computerName
                Accounts     = $_.substring($_.lastindexof("Domain=") + 7).replace("`",Name=`"", "\")
            }
        }
    }
    else {
        [pscustomobject]@{
            ComputerName = $computerName
            Accounts     = "Unable to reach $computername"
        }
    }
}
  
$data = 
foreach ($computer in (Get-ADComputer -filter * | select name).name) {
    Write-Verbose "Checking $computer" -Verbose
    get-localadmins -computerName $computer
}
  
$data | export-csv ".\Report.csv" -NoTypeInformation -Force
1 Spice up

that did the trick! I added a custom export path but evrything worked like a charm. Thanks for the assistance!