Every now and again, I find myself looking up the same things for Office 365 command line administration (i.e. using PowerShell), so it’s probably worth me writing them down in one post…
Of course, a connection to Office 365 from PowerShell is a pre-requisite – although that’s a lot simpler now than it used to be as there’s no longer any need for the Microsoft Online Services Sign In Assistant (MOS SIA), just:
Import-Module MSOnline
$Credential = Get-Credential
Connect-MsolService -credential $Credential
If you’re doing this in a script, you might want to save the password as a secure string (as described in more detail by Kris Powell):
(Get-Credential).Password | ConvertFrom-SecureString | Out-File Password.txt
To use the secure string:
$User = "alias@domainname.tld"
$Pass = Get-Content "Password.txt" | ConvertTo-SecureString
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $user,$pass
Then Connect-MsolService -credential $Credential
as above.
Setting a user password (and making sure you don’t need to force a change – one reason to do it from PowerShell rather than the web portal) involves:
Set-MsolUserPassword -UserPrincipalName alias@domainname.tld -forcechangepassword $false -newpassword password
And, if it’s a service account, turn off password expiry?
Set-MsolUser -UserPrincipalName alias@domainname.tld -PasswordNeverExpires $true