1

I am trying to take an existing CSV file and append it with the output from another command. Whenever I run the script, it sets all values in the column to the $owner.

I'm not sure what I'm missing, but it is not working properly.

`Connect-MicrosoftTeams

#get list of teams
$TeamsFile = Get-Team | Select DisplayName, GroupID, Description | Export-CSV -Path "C:\Scripts\Microsoft_Teams_List.csv" -Delimiter "," -NoTypeInformation

#read row in CSV, add owner to new column.
ForEach ($Team in $TeamsFile) {
$Owner = Get-TeamUser -GroupId $Team.GroupID -Role Owner | Select Name

Write-Host $Team.GroupID " Owner: " $Owner.name

$TeamsFile = Import-CSV -Path "C:\Scripts\Microsoft_Teams_List.csv"
$TeamsFile | Select-Object -Property *, @{label = 'Team Owner'; expression = {$Owner.name}} 
}

$TeamsFile | Export-CSV -Path "C:\Scripts\Microsoft_Teams_List.csv" -Delimiter "," -NoTypeInformation

`

Sample of existint CSV enter image description here

3
  • could you show a part of csv file..it will be easier to trap the error, if we could replay the sample...And display the result waited Commented May 24, 2021 at 16:29
  • I uploaded the image in the main question. Commented May 24, 2021 at 16:40
  • If you want the first $TeamsFile to contain anything, you should use Get-Team | Select DisplayName, GroupID, Description -OutVariable TeamsFile | Export-CSV -Path "C:\Scripts\Microsoft_Teams_List.csv" -Delimiter "," -NoTypeInformation Commented May 24, 2021 at 16:40

2 Answers 2

2

If you only need a merged output of two commands, you may make use of calculated properties:

Get-Team |
    Select DisplayName,GroupID,Description,
        @{n='Team Owner';e={(Get-TeamUser -GroupId $_.GroupID -Role Owner).Name}}

If you already have a CSV file of Team data and need just the owner, you may update your CSV row objects with a new property using a calculated property also:

(Import-CSV -LiteralPath 'C:\Scripts\Microsoft_Teams_List.csv' |
    Select *,@{n='Team Owner';e={(Get-TeamUser -GroupId $_.GroupID -Role Owner).Name}}) |
        Export-Csv -LiteralPath 'C:\Scripts\Microsoft_Teams_List.csv' -NoTypeInformation
    

In your attempt, the following code does not yield the results you think:

$TeamsFile = Get-Team | Select DisplayName, GroupID, Description | Export-CSV -Path "C:\Scripts\Microsoft_Teams_List.csv" -Delimiter "," -NoTypeInformation

$TeamsFile won't contain any output because all output was sent down the pipeline to Export-Csv. If you want to capture output before it is sent to Export-Csv, you can use the common parameter -OutVariable.

Get-Team | Select DisplayName, GroupID, Description -OutVariable TeamsFile |
    Export-CSV -Path "C:\Scripts\Microsoft_Teams_List.csv" -Delimiter "," -NoTypeInformation
$TeamsFile # now contains your Teams
Sign up to request clarification or add additional context in comments.

2 Comments

This worked! Can the same login be applied for a 3rd command? So if you want to find out which 'office' the team is used in, could you add this? I don't think so as it is not working: Get-Team | Select DisplayName,GroupID,Description, @{n='Team Owner';e={(Get-TeamUser -GroupId $_.GroupID -Role Owner).user}} @{n='Office';e={(get-mailbox -identity $_.user).office}} | Export-CSV -Path "C:\Scripts\Microsoft_Teams_List.csv" -Delimiter "," -NoTypeInformation
You need a comma between the calculated property hash tables, but, yes, the same can be applied for a second property. There could be a point where running many commands for each input object will start to become slower than doing larger queries and filtering them down.
0

If headers are the same, then you can keep source csv files in a directory and use this command to generate a combined csv.

Get-ChildItem -Filter *.csv | Select-Object -ExpandProperty FullName | Import-Csv | Export-Csv .\combinedreport.csv -NoTypeInformation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.