Reads a list of users from a txt file and retrieves license info for each account, displays it on screen. Great for migrations when needing to check that all migrated mailboxes or accounts have been licensed correctly.
In summary, the script reads a list of usernames from a file, constructs their respective UPNs, queries Azure AD for each user, and displays their DisplayName and Licenses properties in a formatted list. The purpose of fetching license information might be related to a migration or management task involving user accounts and licenses in an Azure AD environment.
Technical Details
The provided script reads a list of usernames from a file called “migrate_user.txt” located in the “c:\temp\” directory. It then fetches information about these users from Azure Active Directory (Azure AD) and displays their DisplayName and Licenses properties.
Let’s break down the script step by step:
1. The script reads the content of the file “c:\temp\migrate_user.txt” using the `Get-Content` cmdlet. The usernames are stored in the variable `$User`.
2. It retrieves the current date and formats it as “MM/dd/yyyy” using the `Get-Date` cmdlet. The formatted date is stored in the variable `$day`.
3. Constructs a new date and time string by concatenating the value of `$day` with the string ” 5:00 AM”. The resulting string is stored in the variable `$date`. The purpose of this date construction is not explicitly stated in the script, so its exact use is not clear from the given code.
4. The script then enters a `ForEach` loop, where it iterates through each username in the `$User` array. Each username is represented by the variable `$UItem`.
5. Inside the loop, it constructs the User Principal Name (UPN) by concatenating `$UItem` with the domain name “@yourdomain.com”. The UPN is then stored in the variable `$UPN`.
6. It uses the `Get-MsolUser` cmdlet to retrieve information about the user from Azure AD based on the constructed UPN. The `-UserPrincipalName` parameter is used to specify the UPN of the user to fetch.
7. The results of the `Get-MsolUser` cmdlet are then formatted using the `Format-List` cmdlet to display the user’s DisplayName and Licenses properties in a list format. The `Format-List` cmdlet formats the output in a readable way, showing each property on a separate line.
The Script
$User = Get-Content “c:\temp\migrate_user.txt”
$day = Get-Date -Format “MM/dd/yyyy”
$date = $day + ” 5:00 AM”
ForEach ($UItem in $User){
#(complete move, change date to today)
$UPN=($Uitem + “@yourdomain.com”)
Get-MsolUser -UserPrincipalName $UPN | Format-List DisplayName,Licenses
}