Script to add an Office 365 license with custom licensing options

<# .SYNOPSIS Adds Office 365 Licenses to selected users .DESCRIPTION Adds Office 365 Licenses to selected users This script can be executed on any machine that has the MSOnline module installed. .EXAMPLE Add-DOJO365License -EmailAddress Joe.User@company.com -licenses PDT:STANDARDPACK .EXAMPLE Add-DOJO365License -EmailAddress Joe.User@company.com -licenses PDT:ATP_ENTERPRISE,PDT:FLOW_FREE #>
Function Add-DOJO365License {

    [cmdletbinding()]
    param(
        [Parameter(mandatory=$true)]
        [string]$emailaddress,
        [string]$licenses = "PDT:STANDARDPACK"
        
    )

    Begin{    
    Import-Module MSonline    
    }
    Process{    
    Write-Verbose "$licenses"
    Write-Verbose $emailaddress    
    if((Get-MsolUser -UserPrincipalName $emailaddress).islicensed){
        Set-MsolUserLicense -UserPrincipalName $emailaddress -RemoveLicenses $licenses
    }
    if($licenses = "PDT:STANDARDPACK" ){
        $LO = New-MsolLicenseOptions -AccountSkuId $licenses -DisabledPlans  "YAMMER_ENTERPRISE" , "MCOSTANDARD" , "SHAREPOINTSTANDARD" , "SHAREPOINTWAC"
        Set-MsolUser -UsageLocation 'AU' -UserPrincipalName $emailaddress
        Set-MsolUserLicense -UserPrincipalName $emailaddress -LicenseOptions $LO -AddLicenses $licenses -Verbose
        }
    
    else{
        Set-MsolUser -UsageLocation 'AU' -UserPrincipalName $emailaddress
        Set-MsolUserLicense -UserPrincipalName $emailaddress -AddLicenses $licenses -Verbose
        }

    
    }
    End{}
}
$EmailAddress = "sample.user@company.com"
Connect-MsolService
Add-DOJO365License -EmailAddress $emailaddress 

Using PowerShell to connect to Office 365 when a proxy blocks your authentication

Sometimes PowerShell does not have access to the internet because there is a proxy in the way. This can be a problem when you are trying to update help or authenticate to a cloud service provider like Office 365.

The way to get around this is to use a session option.

$cred = Get-Credential
$proxysettings = New-PSSessionOption -ProxyAccessType IEConfig
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri “https://ps.outlook.com/powershell/” `
-Credential $Cred -Authentication Basic -AllowRedirection -SessionOption` $proxysettings Import-PSSession $Session -AllowClobber -Prefix o365

This assumes that you can browse using internet explorer and that your account has the correct access in Office 365. Also in this example I am using the prefix O365 to distinguish this session from the On premises session.