This script uses an Input CSV file to assign a specific license on Office 365. it has to be run through PowerShell by connecting to Exchange Online.
The Input CSV file must be "," delimited and contain the below column header:
- UserPrincipalName
The Usage Location and License type need to be edited in the script (these values are 'hard-coded' into a variable to make it simpler to execute the script without always mentioning the license type in command line)
You can get the license SkuID by running 'Get-MsolAccountSku', and choosing the appropriate license type.<#.SYNOPSISImports user list from CSV file and then assigns license on Office 365.PARAMETER InputFilePath for the Input CSV file.PARAMETER SkipLineOptional parameter for value of line number to start from in CSV file.Does not include count for header row (if you want to skip first user, enter 1).PARAMETER StartTranscriptSwitch to start transcript and store in current directory as text file..DESCRIPTIONTakes an input file with the column 'UserPrincipalName' and first assigns a UsageLocationas per the $UsageLocation variable 'hard-coded' into the script.Then it assigns license to the Office 365 user according to the $License variable also 'hard-coded'into the script in Parameter Declarations section..INPUTSInputFile - CSV File with "," delimited attributes. Must include a column with header'UserPrincipalName'.OUTPUTSassign_LicenseO365-Log - TXT file containing list of all items processed successfullyassign_LicenseO365-Error - TXT file contains list of any errors occured during script runtimeassign_LicenseO365-Transcript - TXT file contains PowerShell transcript (if StartTranscript is used).NOTESVersion: 1.0Author: Sidharth ZutshiCreation Date: 16/11/2017Change Date:Purpose/Change:.EXAMPLEPS C:\> .\assign_LicenseO365.ps1 -InputFile Users.csvRuns script for all users in the input CSV file. Assigns license as specified by $Licensevariable in script.EXAMPLEPS C:\> .\assign_LicenseO365.ps1 -InputFile Users.csv -SkipLine 5Skips the first 5 users in the CSV file and assigns license for all remaining users..EXAMPLEPS C:\> .\assign_LicenseO365.ps1 -InputFile Users.csv -StartTranscriptRuns script for all users in the input CSV file and outputs PS transcript---------------------------------------------------------------------------------------#>[CmdletBinding()]param([Parameter(Mandatory = $True)][ValidateNotNullOrEmpty()][string]$InputFile,[int]$SkipLine = 0,[switch]$StartTranscript = $False)$CurrentDate = (Get-Date -Format "dd-MM-yyyy_HH-mm")$count = 0$errcount = 0#region----------------------------------------------[Parameter Declarations]---------------------------------------------------$OutputLog = ".\assign_LicenseO365-Log_$CurrentDate.txt"$OutputErrorLog = ".\assign_LicenseO365-Error_$CurrentDate.txt"$OutputTranscript = ".\assign_LicenseO365-Transcript_$CurrentDate.txt"$License = "tenant:ENTERPRISEPACK" #Needs to be changed as per your environment SkuID of License$UsageLocation = "IT" #Needs to be changes as per your usage location#endregion#region--------------------------------------------------[Execution Start]-------------------------------------------------------if ($StartTranscript -eq $True){Start-Transcript -Path $OutputTranscript}#region: Add Header to Log Files and OutputWrite-Output "`n`nStarting script ***assign_LicenseO365*** with parameters set as------------------------------------------------------InputFile = $InputFileLicense Type = $LicenseUsage Location = $UsageLocationStartTranscript = $StartTranscriptSkip Users = $SkipLineOutput Log File = $OutputLogOutput Error Log = $OutputErrorLogOutput Transcript = $OutputTranscript------------------------------------------------------"$Current = (Get-Date -Format "dd-MM-yyyy HH:mm:ss")$header = "Script ***assign_LicenseO365***--------------------------------------------------Started on: $CurrentInput File: $InputFileLicense: $LicenseSkip Users: $SkipLine"Write-Verbose "Initializing Log Files and adding Headers..."$header > $OutputLog$header > $OutputErrorLog#endregion#Import CSV file into variable for processingWrite-Verbose "Importing CSV File for list of user UPNs..."$Items = (Import-CSV $InputFile -ErrorAction Stop | Select-Object -Property UserPrincipalName -Skip $SkipLine)#region: Loop to process each mailboxforeach($Item in $Items){$Error.Clear()try{Write-Output "Assigning License to $($Item.UserPrincipalName)"#ASSIGN LICENSE TO USER IN $ITEM$UPN = $Item.UserPrincipalNameWrite-Verbose " [Set-MsolUser] -UsageLocation $UsageLocation"Set-MsolUser -UserPrincipalName $UPN -UsageLocation $UsageLocation -ErrorAction StopWrite-Verbose " [Get-MsolUser | Set-MsolUserLicense] -AddLicenses $License"Get-MsolUser –UserPrincipalName $UPN | Set-MsolUserLicense -AddLicenses “$License”if($Error.Count -ne 0){Write-Host "[ERROR]: Error in assigning license to user! Please see Output Error Logs for details." `-ForegroundColor Red$string = "--------------------------------------------------User = $($Item.UserPrincipalName)Error Details:"$string >> $OutputErrorLog$Error[0] >> $OutputErrorLog$Error[1] >> $OutputErrorLog$errcount++}else{Write-Host "User Licensed successfully." -ForegroundColor Green$string = "--------------------------------------------------Item Processed with details:User = $($Item.UserPrincipalName)"$string >> $OutputLog$count++}$Error.Clear()}catch{Write-Host "[ERRORCATCH]: Error in assigning license to user! Please see Output Error Logs for details." `-ForegroundColor Red$string = "--------------------------------------------------User = $($Item.UserPrincipalName)Error Details:"$string >> $OutputErrorLog$Error[0] >> $OutputErrorLog$Error[1] >> $OutputErrorLog$errcount++}finally{$TotalCount = $count + $errcountif($TotalCount -ne 1){$Percent = "{0:N2}" -f ($TotalCount/$Items.count * 100)Write-Progress -Activity "Assigning Licenses..." `-Status "Progress: $Totalcount/$($Items.count) $Percent% " `-PercentComplete $Percent `-CurrentOperation "$($Item.SomeProperty)"}}}#endregionif ($StartTranscript -eq $True){Stop-Transcript}#endregion#region------------------------------------------------[End Processing]-----------------------------------------------------------#region: Add footer to Log files and Output$CurrentEnd = (Get-Date -Format "dd-MM-yyyy HH:mm:ss")Write-Output "`n`n`n**************************End Script**************************`n`n"Write-Output "Script Ended on $CurrentEndTotal Items Processed = $countTotal Errors = $errcount"$footerLog = "----------------------------------------------------------------------------------------------------********************END SCRIPT********************Script Ended on: $CurrentEndTotal Items Processed: $count"$footerError = "----------------------------------------------------------------------------------------------------********************END SCRIPT********************Script Ended on: $CurrentEndTotal Errors: $errcount"Write-Verbose "Adding Footer to Log Files..."$footerLog >> $OutputLog$footerError >> $OutputErrorLog#endregion#endregion#--------------------------------------------------------------***End Script***----------------------------------------------------------