CodexBloom - Programming Q&A Platform

PowerShell 7.3 - implementing Custom Function Returning Object but Not Displaying in Pipeline

👀 Views: 15 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-10
powershell ad custom-objects PowerShell

I'm integrating two systems and This might be a silly question, but I'm working with a frustrating scenario in PowerShell 7.3 where my custom function is returning an object, but when I try to use it in the pipeline, it doesn't display or behave as expected... Here's my function which is supposed to take a list of users and return their details as custom objects: ```powershell function Get-CustomUserDetails { param ( [string[]]$Usernames ) foreach ($username in $Usernames) { $user = Get-ADUser -Identity $username -Properties DisplayName, EmailAddress [PSCustomObject]@{ Name = $user.DisplayName Email = $user.EmailAddress } } } ``` When I call the function like this: ```powershell Get-CustomUserDetails -Usernames "user1", "user2" | Format-Table ``` I expected to see a formatted table with names and email addresses, but instead, I get no output. I've tried adding `Write-Output` at the end of the function, but it still doesn't work. Additionally, when I run the function directly without piping it, like this: ```powershell Get-CustomUserDetails -Usernames "user1", "user2" ``` I can see the results in the console, but they just don't get piped correctly. I checked if my `$Usernames` parameter is populated correctly, and it seems fine. Could it be an scenario with how I'm constructing the custom object? Or is it related to how PowerShell handles pipeline output from functions? Any insights or fixes would be greatly appreciated! Is there a better approach? Am I approaching this the right way?