Office 365 Powershell commands
Not being overly familiar with powershell I keep this list to remeber how to do stuff and help do new stuff.
I reproduce the list to help anyone else looking for the right command
Connect to Office365
Connect-MsolService
connect to exchange online
$UserCredential = Get-Credential $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri [url]https://outlook.office365.com/powershell-liveid/[/url] -Credential $UserCredential -Authentication Basic -AllowRedirection Import-PSSession $Session -DisableNameChecking **if you need to enable running scripts set-executionpolicy remotesigned
Export staff emails from office365
get-msoluser -all | where-object {$_.userprincipalname -like "*@domain*"} | select-object userprincipalname | export-csv -path "d:\staffemails.csv"
Computers in AD and their last logon date
get-adcomputer -filter * -properties Name,LastLogonDate | sort LastLogonDate | FT Name,LastLogonDate -autosize | out-file "d:\allcomputers.txt"
Printers (not sure what this for)
get-wmiobject -class win32_printer -computername printserver -Filter "name='sci-colour'"
Add staff to security group
get-msoluser -all | where-object {$_.userprincipalname -like "*@domain*"} | export-csv -path "d:\staffemails.csv"
$securitygroup = get-msolgroup | where-object {$_.displayname -eq "Encrypted_mail_users"}
import-csv d:\staffemails.csv | Foreach {add-msolgroupmember -groupobjectid $securitygroup.objectid -groupmembertype "User" -groupmemberobjectid $_.objectid}
add staff to mail enabled group using exchange online
make csv with msol.
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri [url]https://outlook.office365.com/powershell-liveid/[/url] -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session -DisableNameChecking
import-csv "d:\staffemails.csv" | foreach {add-distributiongroupmember -identity "groupname" -member $_.userprincipalname}
Remove a user or many users
#userprincipalname is usually their email address. CSV needs userprincipalname as column name
import-csv d:\tobedeleted.csv | foreach {remove-msoluser -userprincipalname $_.userprincipalname -force}
Assign license to many users
Set usage location first
import-csv d:\2019emails.csv | foreach {set-msoluser -userprincipalname $_.userprincipalname -usagelocation GB}
import-csv d:\2019emails.csv | foreach {set-msoluserlicense -userprincipalname $_.userprincipalname -addlicenses <tenantID>:STANDARDWOFFPACK_IW_STUDENT}
(it has an invalid error if the license is already assigned)
Add office field(admission number) from AD to many users
the column headings in the csv can be referenced with $_.
import-csv pupils.csv | foreach {set-msoluser -userprincipalname $_.emailaddress -office $_.officecolumn}
Account SKU IDs for students
get-msolaccountsku
AccountSkuId ActiveUnits WarningUnits ConsumedUnits
------------ ----------- ------------ -------------
<tenantID>:STANDARDWOFFPACK_STUDENT 3000 0 893
<tenantID>:STANDARDWOFFPACK_IW_STUDENT 1000000 0 661 (this one includes office apps)
<tenantID>:STANDARDWOFFPACK_IW_FACULTY staff with office365 apps
Get a list of users' UPNs
get-msoluser -all | where-object {$_.userprincipalname -like "*domain*"} | select-object userprincipalname,displayname | export-csv -path "d:\allemail.csv"
get last useractivity time
import-csv "D:\allemail.csv" | foreach {$UPN = $_.UserPrincipalName; get-mailboxstatistics -identity $UPN | select @{n='UserPrincipalName';e={$UPN}},displayname,lastuseractiontime}
Need to do "Get a list of users' UPNs" above to get the UPNs
Remove groups using exchange online
export groups from admin groups list
import csv groups.csv | foreach {remove-unifiedgroup -identity $_.groupemail -confirm:$False}
Set the name of ADusers the same as sims "preferred name"
import-csv .\update.csv | foreach {set-aduser -identity $_.UserName -givenname $_.Forename -surname $_.Surname -displayname $_.Fullname}
set-aduser $_.Username -passthru | rename-adobject -newname $_.Fullname
Change the name of office365 accounts to match sims "preferred name"
set-msoluser -UserPrincipalName [email][email protected][/email] -FirstName "Fred" -Lastname "Bloggs" -DisplayName "Fred Bloggs" Import-Csv .\update.csv | foreach {set-msoluser -UserPrincipalName $_.EMail -FirstName $_.Forename -Lastname $_.Surname -DisplayName $_.Fullname}

1 Comment
Recommended Comments
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now