Output csv file shows length not value

lee roberts 186 Reputation points
2024-02-29T15:57:44.77+00:00

I have a csv file with 18,000 users in. I'm trying to match the criteria of, the account is enabled and is one of 3 groups. The script i'm using is below, and doesn't give me any errors. But the CSV file has a header "length" and then a number. What i'm want is Username and write message output. Can anyone assist with why this is happening?

# Replace the path with the actual location of your CSV file
$csvPath = "C:\Testuser.csv"
# Specify the three target groups (replace with your desired group names)
$group1 = "Group1"
$group2 = "Group2"
$group3 = "Group3"
# Import the CSV and iterate through each user
Import-Csv $csvPath | ForEach-Object {
    $userUPN = $_.Username
    try {
        # Check if the user account exists in Active Directory
        $adUser = Get-ADUser -Filter "UserPrincipalName -eq '$userUPN'" -ErrorAction Stop -Properties Enabled, MemberOf
        # Check if the user is enabled
        if ($adUser.Enabled) {
            # Check if the user is a member of at least one of the target groups
            if ($adUser.MemberOf -like "*$group1*" -or
                $adUser.MemberOf -like "*$group2*" -or
                $adUser.MemberOf -like "*$group3*") {
                Write-Output "{$userUPN}: Criteria matches (User is enabled and in at least one of the specified groups)."
            } else {
                Write-Output "{$userUPN}: Criteria doesn't match (User is enabled but not in any of the specified groups)."
            }
        } else {
            Write-Output "{$userUPN}: Criteria doesn't match (User account is disabled)."
        }
    } catch {
        Write-Output "{$userUPN}: AD account not found."
    }
} | Export-Csv -Path "C:\Results.csv" -NoTypeInformation

PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,445 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 46,551 Reputation points
    2024-02-29T16:19:19.4033333+00:00

    The Export-CSV expects to receive an object with named properties, not a simple string value. See if this works for you:

    # Replace the path with the actual location of your CSV file
    $csvPath = "C:\Testuser.csv"
    # Specify the three target groups (replace with your desired group names)
    $group1 = "Group1"
    $group2 = "Group2"
    $group3 = "Group3"
    # Import the CSV and iterate through each user
    $TheData = @{UPN="";Result=""}
    Import-Csv $csvPath | ForEach-Object {
        $userUPN = $_.Username
        $TheData.UPN = $_.Username
        try {
            # Check if the user account exists in Active Directory
            $adUser = Get-ADUser -Filter "UserPrincipalName -eq '$userUPN'" -ErrorAction Stop -Properties Enabled, MemberOf
            # Check if the user is enabled
            if ($adUser.Enabled) {
                # Check if the user is a member of at least one of the target groups
                if ($adUser.MemberOf -like "*$group1*" -or
                    $adUser.MemberOf -like "*$group2*" -or
                    $adUser.MemberOf -like "*$group3*") {
                        $theData.Result = "Criteria matches (User is enabled and in at least one of the specified groups)."
    #                   Write-Output "{$userUPN}: Criteria matches (User is enabled and in at least one of the specified groups)."
                } else {
                    $TheData.Result = "Criteria doesn't match (User is enabled but not in any of the specified groups)."
    #               Write-Output "{$userUPN}: Criteria doesn't match (User is enabled but not in any of the specified groups)."
                }
            } else {
                $TheData.Result ="Criteria doesn't match (User account is disabled)."
    #           Write-Output "{$userUPN}: Criteria doesn't match (User account is disabled)."
            }
        } catch {
            $TheData.Result ="{$userUPN}: AD account not found."
    #       Write-Output "{$userUPN}: AD account not found."
        }
        [PSCustomObject]$TheData
    } | Export-Csv -Path "C:\Results.csv" -NoTypeInformation
    
    
    I left the original "Write-Output" cmdlets in the code, but turned them into comments.
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.