Run several Powershell commands one after the other

Viewed 47

I'm trying to run several Powershell commands one after the other, but it seems like only first three work: "Write-host", "Connect-MicrosoftTeams" and "Import-Csv -Path "C:\Users\Desktop\smith.csv" | foreach{Add-TeamUser -GroupId XXX -user $_.email}"

My script:

Write-host "Login to Microosft teams";
#Import and connect to Microsoft Teams
Import-Module -Name 'MicrosoftTeams'
Connect-MicrosoftTeams
Import-Csv -Path "C:\Users\Desktop\smith.csv" | foreach{Add-TeamUser -GroupId XXX -user $_.email}
if ($?) {
    Import-Csv -Path “C:\Users\Desktop\smith.csv” | foreach{Add-TeamChannelUser -GroupId XXX -DisplayName "XXX" -user $_.email}
}
if ($?) {
    Write-host "Users have been added"
}

How does it work? What should I do to run few commands one after the other? Thanks for the help!

1 Answers

You only need a single ForEach-Object call - you can add multiple statements to the script block:

Write-host "Login to Microosft teams";
#Import and connect to Microsoft Teams
Import-Module -Name 'MicrosoftTeams'
Connect-MicrosoftTeams
Import-Csv -Path "C:\Users\Desktop\smith.csv" |ForEach-Object {
    Add-TeamUser -GroupId XXX -user $_.email
    Add-TeamChannelUser -GroupId XXX -DisplayName "XXX" -user $_.email
    # ... etc.
}
Related