Prebuilt PowerShell scripts

IniobongNkanga-8038 616 Reputation points
2024-09-26T15:17:18.4266667+00:00

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:

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,943 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,517 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,489 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.