PowerShell: Office 365 Bulk Licensing, Changing IDs, Enabling POP3

Connect to Office 365
# Office 365 Global Admin Credential
$username="O365globalAdmin"
$password=ConvertTo-securestring "PASSWORDHERE" -AsPlainText -Force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username,$password
#$cred = Get-Credential

# Connect to Office 365
if (!(Get-Module -ListAvailable -NameMSOnline)){Install-Module MSOnline -Confirm:$false -Force;}
# Install-Module AzureAD -Confirm:$false -Force # Azure AD may not be necessary for managing O365
$O365Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $cred -Authentication Basic -AllowRedirection
Import-PSSession $O365Session -AllowClobber
Connect-MsolService -Credential $cred
# Check for Licenses
PS C:\Windows\system32> Get-MSOLAccountSKU
AccountSkuId ActiveUnits WarningUnits ConsumedUnits
------------ ----------- ------------ -------------
kimconnect:STANDARDWOFFPACK_IW_FACULTY 500000 0 132
kimconnect:WINDOWS_STORE 25 0 0
kimconnect:STANDARDWOFFPACK_STUDENT 500 0 1
kimconnect:STANDARDWOFFPACK_IW_STUDENT 1000000 0 2
kimconnect:FLOW_FREE 10000 0 3
kimconnect:OFFICESUBSCRIPTION_FACULTY 100 0 7
kimconnect:OFFICESUBSCRIPTION_STUDENT 4000 0 0
kimconnect:STANDARDWOFFPACK_FACULTY 300 0 12
# Get a list of all unlicensed users 
Get-MsolUser -All -UnlicensedUsersOnly

PS C:\Windows\system32> Get-MsolUser -All -UnlicensedUsersOnly
UserPrincipalName DisplayName isLicensed
----------------- ----------- ----------
kim.connect@kimconnect.org Kim Connect False
bruce.lee@cmcharter.org Bruce Lee False
chuck.norris@cmcharter.org Chuck Norris False
# Apply license to 1 user
$license="kimconnect:STANDARDWOFFPACK_IW_FACULTY"
$userPrincipleName="admin@kimconnect.com"
Set-MSOLUser -UserPrincipalName $userPrincipleName –UsageLocation "US"; Set-MSOLUserLicense -UserPrincipalName $userPrincipleName –AddLicenses $license
# Create a CSV report of email users and sort them by Last Logon Time Stamp
$rawReport=Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails UserMailbox,SharedMailbox | Get-MailboxStatistics | Select-Object DisplayName,ItemCount,LastLogonTime,ServerName | Sort-Object lastlogontime -Descending
$rawReport | Export-Csv -Path C:\Temp\mailbox-users-report.csv -NoTypeInformation
# Apply license to a list of users prepared in a text file
$license="kimconnect:STANDARDWOFFPACK_IW_FACULTY"
$textfile="C:\Temp\userslist.txt"
Get-Content $textfile | ForEach {Set-MSOLUser -UserPrincipalName $_.UserPrincipalName –UsageLocation "US"; Set-MSOLUserLicense -UserPrincipalName $_.UserPrincipalName –AddLicenses $license}
Apply Licensing to All Unlicensed Users
# Apply license to all unlicensed users
$license="myelement:STANDARDWOFFPACK_IW_FACULTY"
$unlicensedUsers=Get-MsolUser -All -UnlicensedUsersOnly
$unlicensedUsers | ForEach {Set-MSOLUser -UserPrincipalName $_.UserPrincipalName –UsageLocation "US"; Set-MSOLUserLicense -UserPrincipalName $_.UserPrincipalName –AddLicenses $license}
Enable POP3 & IMAP (Default)
# Office 365 Global Admin Credential
$username="tupac@kimconnect.org"
$password=ConvertTo-securestring "PASSWORDHERE" -AsPlainText -Force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username,$password
#$cred = Get-Credential

# Connect to Office 365
if (!(Get-Module -ListAvailable -NameMSOnline)){Install-Module MSOnline -Confirm:$false -Force;}
# Install-Module AzureAD -Confirm:$false -Force # Azure AD may not be necessary for managing O365
$O365Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $cred -Authentication Basic -AllowRedirection
Import-PSSession $O365Session -AllowClobber
Connect-MsolService -Credential $cred
# Enable POP3 & IMAP for all accounts
$users=Get-MsolUser
$users | % {Set-CASMailbox -Identity $_.UserPrincipalName -POPEnabled $true -IMAPEnabled $true}
Set Primary Login IDs
# Change Login ID for 1 account
$id="test@kimconnect.org"
$newID="test@kimconnect.com"
Set-MsolUserPrincipalName -UserPrincipalName $id -NewUserPrincipalName $newID
# Set Login ID for all accounts
$users=Get-MsolUser
$regexSelectUsername = '^[^@]+'
$regexAdSyncUsername = 'Sync_*'
$domain="myelement.org"
foreach ($user in $users){
$username=$user.UserPrincipalName;
$username -match $regexSelectUsername > $null;
$match=$matches[0];
$matchedAdSyncUsername=$match -like $regexAdSyncUsername
if (!($matchedAdSyncUsername)){
$newId="$match@$domain"
#"$username $newID"
Set-MsolUserPrincipalName -UserPrincipalName $username -NewUserPrincipalName $newID;
}
#pause;
}
# Add user login
$id="test@kimconnect.org"
Set-Mailbox $id -EmailAddresses @{add="tupac@kimconnect.com"}

Output:
PS C:\Windows\system32> Set-Mailbox "tupac@kimconnect.onmicrosoft.com" -EmailAddresses @{add="tupac@kimconnect.com"}
The operation on mailbox "Ruth Flores" failed because it's out of the current user's write scope. The action
'Set-Mailbox', 'EmailAddresses', can't be performed on the object 'Ruth Flores' because the object is being
synchronized from your on-premises organization. This action should be performed on the object in your on-premises
organization.
+ CategoryInfo : InvalidOperation: (Ruth Flores:ADObjectId) [Set-Mailbox], InvalidOperationException
+ FullyQualifiedErrorId : [Server=BYAPR04MB3928,RequestId=992d520c-f0fe-4e4b-8aec-8abd4c14528a,TimeStamp=6/28/2019
5:39:25 AM] [FailureCategory=Cmdlet-InvalidOperationException] 9CAD9558,Microsoft.Exchange.Management.RecipientTa
sks.SetMailbox
+ PSComputerName : ps.outlook.com

Leave a Reply

Your email address will not be published. Required fields are marked *