Forum Discussion
Pierre-Yves_Letournel
Jan 17, 2024Copper Contributor
find the subcribed date of a license per user
Hello, I'm in charge of the MS35 administration for a non-profit and non-governmental Organization. We've got several local services and I need to report to each local service the cost of the licens...
ProSolutions
Jan 28, 2024Iron Contributor
Hello Pierre-Yves, To retrieve historical license information, including subscription and end dates, you can use PowerShell and Microsoft Graph API. Here is a basic outline of the steps you can follow:
### Prerequisites:
1. Ensure you have the required permissions to access Microsoft Graph API.
2. Install the MSOnline module for PowerShell (if not installed).
### PowerShell Script:
```powershell
# Install the MSOnline module (if not installed)
# Install-Module -Name MSOnline -Force -AllowClobber
# Import MSOnline module
Import-Module MSOnline
# Connect to MSOnline
$credential = Get-Credential
Connect-MsolService -Credential $credential
# Get all users
$users = Get-MsolUser -All
foreach ($user in $users) {
$userPrincipalName = $user.UserPrincipalName
# Get license information for the user
$licenses = Get-MsolUser -UserPrincipalName $userPrincipalName | Select-Object -ExpandProperty Licenses
foreach ($license in $licenses) {
$serviceName = $license.AccountSkuId
$subscribedDate = $license.SubscribedPlans[0].AssignedTimestamp
$endDate = $license.SubscribedPlans[0].ExpiryTimestamp
# Output the information (you can modify this part as needed)
Write-Output "User: $userPrincipalName, Service: $serviceName, Subscribed Date: $subscribedDate, End Date: $endDate"
}
}
# Disconnect from MSOnline
Disconnect-MsolService
```
### Notes:
- Modify the output section based on your reporting needs.
- Ensure you have the necessary permissions to execute these commands.
- Make sure to handle authentication securely, especially if you plan to schedule or automate the script.
This script uses the MSOnline PowerShell module to connect to MS 365 and retrieve license information for each user. You may need to adjust it based on your specific requirements and environment.
Remember to test the script in a safe environment before running it in production. Additionally, Microsoft Graph API may provide more advanced capabilities for historical data, but it requires more complex authentication and permission handling.
Have a great day lo Pierre-Yve!
### Prerequisites:
1. Ensure you have the required permissions to access Microsoft Graph API.
2. Install the MSOnline module for PowerShell (if not installed).
### PowerShell Script:
```powershell
# Install the MSOnline module (if not installed)
# Install-Module -Name MSOnline -Force -AllowClobber
# Import MSOnline module
Import-Module MSOnline
# Connect to MSOnline
$credential = Get-Credential
Connect-MsolService -Credential $credential
# Get all users
$users = Get-MsolUser -All
foreach ($user in $users) {
$userPrincipalName = $user.UserPrincipalName
# Get license information for the user
$licenses = Get-MsolUser -UserPrincipalName $userPrincipalName | Select-Object -ExpandProperty Licenses
foreach ($license in $licenses) {
$serviceName = $license.AccountSkuId
$subscribedDate = $license.SubscribedPlans[0].AssignedTimestamp
$endDate = $license.SubscribedPlans[0].ExpiryTimestamp
# Output the information (you can modify this part as needed)
Write-Output "User: $userPrincipalName, Service: $serviceName, Subscribed Date: $subscribedDate, End Date: $endDate"
}
}
# Disconnect from MSOnline
Disconnect-MsolService
```
### Notes:
- Modify the output section based on your reporting needs.
- Ensure you have the necessary permissions to execute these commands.
- Make sure to handle authentication securely, especially if you plan to schedule or automate the script.
This script uses the MSOnline PowerShell module to connect to MS 365 and retrieve license information for each user. You may need to adjust it based on your specific requirements and environment.
Remember to test the script in a safe environment before running it in production. Additionally, Microsoft Graph API may provide more advanced capabilities for historical data, but it requires more complex authentication and permission handling.
Have a great day lo Pierre-Yve!
- Pierre-Yves_LetournelJan 28, 2024Copper Contributor