I have a variable that loops through values from a CSV file. As the loop returns result sets for each value I have in the CSV, I also want that value (the variable) returned in the result set. I tried to add my variable, $user
, in my Select
statement, but it just shows in the column header and my value is either blank or {}
.
Here's the loop code:
$users = Get-Content C:\AD\ad_groups.txt
ForEach ($User in $users)
{
Get-ADGroupMember -Identity $user -Server MyServerName| Select name,SamAccountName,$user
}
You likely need to use what's called a calculated property. In place of $User, try the following hash table as a property value. "Name" can be shortened to "N" and "Expression to "E" (without the double quotes as seen here), if you feel it's necessary/preferred.
@{Name='User';Expression={$User}}