<# Author: Kulverstukas Website: http://9v.lt Date: 2016-02-01 Last update: nil Description: Lists all mailboxes imported into Office365 which are unlicensed and adds a license to them and sets their location. All users are filtered by name.surname template and BlockCredential which is True for mailgroups to disable sign-in. Reference: http://www.codetwo.com/admins-blog/how-to-connect-and-remotely-manage-office-365-with-powershell/ http://www.codetwo.com/admins-blog/how-to-add-and-license-users-in-bulk-on-office-365/ #> Import-Module MSOnline # --------------------- define stuff ---------------------- $user = "someuser@somecompany.onmicrosoft.com" $passwd = ConvertTo-SecureString "l33tpasswd" -AsPlainText -Force $licSrv = "somecompany:STANDARDWOFFPACK_FACULTY" $studLicSrv = "somecompany:STANDARDWOFFPACK_STUDENT" # --------------------------------------------------------- # do the login here $creds = New-Object System.Management.Automation.PSCredential $user, $passwd $psSession = New-PSSession –ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/PowerShell-LiveID?PSVersion=2.0 -Credential $creds -Authentication Basic -AllowRedirection Import-PSSession $psSession -AllowClobber | Out-Null # suppress output pl0x Connect-MsolService -Credential $creds # dem creds doe $users = Get-MsolUser -UnlicensedUsersOnly -Domain "domain.lt" -All foreach ($user in $users) { if (($user.userPrincipalName -like "*.*") -and ($user.BlockCredential -eq $false)) { # BlockCredential is true for mailgroups, false for users Write-Host "Found $($user.userPrincipalName), adding location and license" Set-MsolUser -UserPrincipalName $user.userPrincipalName -UsageLocation "LT" Set-MsolUserLicense -UserPrincipalName $user.userPrincipalName -AddLicenses $licSrv Set-Clutter -Identity $user.userPrincipalName -Enable $False } } # $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")