Prebuilt PowerShell scripts
Hello
Please i need your help on this issue.
Please do we have any prebuilt PowerShell scripts from Microsoft that can identify teams groups that contain files?
Here’s the one we had tried that isn’t working:
# Connect to Microsoft Graph
Connect-MgGraph -Scopes "Group.Read.All", "Files.Read.All"
# Output CSV file path
$csvFilePath = "TeamsOwnerlessGroupsWithFiles.csv"
# Initialize an array to hold the results
$exportData = @()
# Get all groups that are Microsoft Teams (Teams are groups with 'Team' in resourceProvisioningOptions)
$teamsGroups = Get-MgGroup -Filter "resourceProvisioningOptions/Any(x:x eq 'Team')" -All
# Filter for ownerless Teams groups with files
$ownerlessGroupsWithFiles = $teamsGroups | Where-Object {
($_.owners.Count -eq 0) -and
(Get-MgDrive -GroupId $_.Id) -and
(Get-MgDriveItem -DriveId (Get-MgDrive -GroupId $_.Id).Id -DriveItemId "root")
}
# Loop through each ownerless group with files
foreach ($group in $ownerlessGroupsWithFiles) {
# Initialize a hashtable for storing the information to be exported
$groupData = @{
"Group Name" = $group.DisplayName
"Group ID" = $group.Id
"Has Files" = $true
}
# Get the group members
$members = Get-MgGroupMember -GroupId $group.Id | Select-Object DisplayName, UserType
# Collect member information
if ($members.Count -gt 0) {
$groupData["Members"] = $members | ForEach-Object { "$($.DisplayName) ($($.UserType))" } -join "; "
} else {
$groupData["Members"] = "No Members"
}
# Add the group data to the export array
$exportData += New-Object PSObject -Property $groupData
}
# Export the data to CSV
$exportData | Export-Csv -Path $csvFilePath -NoTypeInformation
Write-Host "Export complete. File saved at $csvFilePath"
We get the following error when running it:
Get-MgDrive:
Line |
3 | (Get-MgDrive -GroupId $_.Id) -and
| ~~~~~~~~
| A parameter cannot be found that matches parameter name 'GroupId'.
Get-MgDrive: