Bankesy Posted July 27, 2023 Posted July 27, 2023 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
Steve21 Posted July 27, 2023 Posted July 27, 2023 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
Bankesy Posted July 27, 2023 Author Posted July 27, 2023 @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
TechMonkey Posted July 27, 2023 Posted July 27, 2023 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 1
Bankesy Posted July 27, 2023 Author Posted July 27, 2023 @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'?
Bankesy Posted July 27, 2023 Author Posted July 27, 2023 @TechMonkey Actually if I import my CSV before running your script it does do something but it's going off and searching for way more than the 15 or so Teams in the CSV
TechMonkey Posted July 27, 2023 Posted July 27, 2023 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? 1
Bankesy Posted July 28, 2023 Author Posted July 28, 2023 Thanks @TechMonkey I'll give it a go, I think I just worried when I saw it searching for more than the amount of Teams in the CSV.
TechMonkey Posted July 28, 2023 Posted July 28, 2023 No worries, yell if you have any other questions. Not a PowerShell guru, but manage to muddle through and trying to do more and more via it. 1
Bankesy Posted July 28, 2023 Author Posted July 28, 2023 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
TechMonkey Posted July 28, 2023 Posted July 28, 2023 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. 1
Bankesy Posted July 28, 2023 Author Posted July 28, 2023 @TechMonkey it works perfectly, thank you so much. This is going to make such a big difference to what we are trying to achieve over here
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now