Jump to content

Recommended Posts

Posted

Good afternoon everybody,

 

Does anybody know if there is a command or script I can run to retrieve and export the Teams Group ID for a bunch of Teams that I have the names of?

 

Thanks in advance

Posted

If you mean one by one it’s just get-team -displayname XYZ

 

Or you could just export the entire lot to a Csv if you want all of them

 

Steve

Posted

@Steve21

 

I've got all of my Teams names with their owners and I've got all of my Teams names with their GroupID but not together, from the list of Team names I've taken all of the ones I want to delete (for which I need the GroupID unless I'm mistaken) so now I want to export the GroupID for only the Teams I want to delete.

 

Thanks

Posted

On some scripts I have got I use a CSV with the teams listed then:

 

Foreach ($Team in $Teams) {

$teamFound = Get-Team -Archived $false -DisplayName $Team.Name

 

Write-Host "Team Found! ID:" $teamFound.GroupID -foregroundColor Green

 

}

 

The only caveat is if you have multiple teams with the same name only the first will be used. So maybe put a check in to error out if results returned >1

  • Thanks 1
Posted

@TechMonkey

 

I'm not very good with PowerShell, I've got a CSV with just one column that has a header and the Teams names below it. I've got as far as a command that imports the CSV and writes the Teams names to a variable but now I am confused as to how I get it to go off and get the GroupId for each of those?

 

I'm guessing in your script there you are importing the CSV to the $Teams variable and your column in the CSV is called 'Team'?

Posted

So if you have the CSV you just have to import it:

 

$Teams = Import-CSV -Path "YOUR FILEPATH HERE - OTHER FILEPATHS ARE AVAILABLE"

 

Use your header after the dot to get the column. So if your column header is TeamName:

 

Foreach ($Team in $Teams) {

$teamFound = Get-Team -Archived $false -DisplayName $Team.TeamName

 

#Put code here for what you want done with each team. Below will just write out the ID.

Write-Host "Team Found! ID:" $teamFound.GroupID -foregroundColor Green

 

}

 

It will be slow as it will search through all your teams to find matches to the display name. AFAIK there is no faster way.

If you think something is going wrong what you can do is type $teamFound after the script is finished and it will show you the last record it was processing. May give a hint at what is happening, for instance, if it returns 2 records, you know your team name isn't unique.

Is it returning more IDs than number of teams you have in your CSV?

  • Thanks 1
Posted

It works @TechMonkey

 

Is there a way I can get it to write the GroupID to a CSV please? Trying this myself so far has yielded the results of being asked for an input object and a blank CSV being created :)

Posted

At the start of the foreach loop put "$Output =" so it should look something like:

$Output =Foreach ($Team in $Teams) {

 

After, or instead of, the Write-Host command add:

 

New-Object -TypeName PSObject -Property @{

TeamID = $teamFound.GroupID

TeamName = $teamFound.DisplayName

TeamMail = $teamFound.MailNickName

} | Select-Object TeamID,TeamName,TeamMail

 

This stores the details you want into an object. You can remove the TeamName and TeamMail bits if you want, just makes it a bit more useful.

 

Then after the curly bracket, out of the foreach loop add:

$Output | Export-Csv "c:\random\scripts\groupIdExport.csv"

 

Changing the path to where ever you want it to save.

I've not tested this but it should do what you want. Let me know how it goes.

  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...