Joe Alexander
Joe Alexander

Reputation: 1

Is there a way in Powershell to append or prepend the value of a variable to the output of a piplined command

I have the one-liner:

Get-ADPrincipalGroupMembership $username | select name | where {$_.name -like 'HI_*'}  

and I would like to add the value of $username to every row of the output, and get the output in CSV format using ConvertTo-CSV (or similar).

e.g., output would look like this:

HI_Users,JBloggs
HI_Supervisors,JBloggs
HI_Admins,JBloggs

This is needed for downstream processing on another system.

Upvotes: 0

Views: 145

Answers (1)

Matt
Matt

Reputation: 46710

Use a calculated property where you would include your variable.

Get-ADPrincipalGroupMembership $username | 
    where-object{$_.name -like 'HI_*'} | 
    select @{label="username";Expression={$username}},name

No need to do anything fancy with that either for CSV output. Just pipe to Export-CSV

Upvotes: 2

Related Questions