Forum Discussion

Eve_X's avatar
Eve_X
Copper Contributor
Jan 20, 2023

Add a user to multiple channels via Powershell

Hi,

 

I'm trying to customize a Powershell script to add one user to multiple channels at once. It will save a lot of work when there's a new employee.

 

This is my original script:

$channels = Import-Csv -Path “C:\Users\Administrator\Desktop\channels.csv” | select -ExpandProperty Channel
$grps=Import-Csv -Path “C:\Users\Administrator\Desktop\channels.csv” | select -ExpandProperty groupID
foreach($channel in $channels){
foreach($grp in $grps){
Add-TeamChannelUser -GroupId $grp -DisplayName "$channel" -user <>
}
}

 

It works flawlessly when the channel's display name doesn't have a space in between. If it does, there will be this error message "Channel not found". I tired the quotation marks but with no luck.

 

How can I make this work for channels with a space in the display name?

 

Any help will be much appreciated!

 

2 Replies

  • Space should not be a problem, and it should work even if you don't add the quotes. I'd suggest double-checking the channel name itself.
    • Eve_X's avatar
      Eve_X
      Copper Contributor
      Thanks Vasil,

      I have figured out it myself by switching to the cmdlets below:

      $channels = Import-Csv -Path “C:\Users\Administrator\Desktop\channels.csv”
      foreach ($channel in $channels)
      {
      $displayname = $channel.Channel
      Add-TeamChannelUser -GroupId $channel.groupid -DisplayName "$displayname" -user <>
      }

      But thanks anyway!

Resources