I have been working on a PS Script that will look in to M365 for a list of users who hasn't logged in for over 90 days and then provide a list of their license so we can see where a potential licenses are being wasted. I have managed to get this far with it at the momenet based on googling and general messing about. If anyone can provide some help that would be great.
#Parameters$InactiveDays = 90 $CSVPath = "C:\InactiveUsers.csv"$InactiveUsers = @()$ThresholdDate = (Get-Date).AddDays(-$InactiveDays) #Connect to Microsoft GraphConnect-MgGraph -Scopes "AuditLog.Read.All", "User.Read.All" -NoWelcome #Set the Graph Profile#Select-MgProfile Beta #Properties to Retrieve$Properties = @( 'Id','DisplayName','Mail','UserPrincipalName','UserType', 'AccountEnabled', 'SignInActivity', 'Department', 'Created', 'AssignedLicenses' ) #Get All users along with the properties$AllUsers = Get-MgUser -All -Property $Properties ForEach ($User in $AllUsers){ $UserLicenseDetails = Get-MgUserLicenseDetail -UserId $user.Id $UserLicenses = $UserLicenseDetails | ForEach-Object { ($_.ServicePlans | ForEach-Object { $_.SkuPartNumber }) -join ', ' } $LastLoginDate = $User.SignInActivity.LastSignInDateTime If($LastLoginDate -eq $null) { $LastSignInDate = "Never Signed-in!" } Else { $LastSignInDate = $LastLoginDatel } #Collect data If(!$LastLoginDate -or ($LastLoginDate -and ((Get-Date $LastLoginDate) -lt $ThresholdDate))) { $InactiveUsers += [PSCustomObject][ordered]@{ LoginName = $User.UserPrincipalName Email = $User.Mail DisplayName = $User.DisplayName UserType = $User.UserType AccountEnabled = $User.AccountEnabled LastSignInDate = $LastSignInDate Department = $User.Department License = $UserLicense } }} $InactiveUsers#Export Data to CSV$InactiveUsers | Export-Csv -Path $CSVPath -NoTypeInformation