windows_wiki:powershell_office_365

Powershell Office 365

General Information

This Wiki will provide a basic overview of common commands to be used for office 365 power shell work. Copy and past all commands in a given section as they are seen in bold. Make sure you run all commands in power shell, and that you launch it as an administrator.

As I work with power shell I will add commands in here.

Checklist

  • Powershell CLI installed

Connecting Office 365 Exchange Power Shell Session (do this before anything below)

The first command requires the Global Administrator account credentials from Office 365 to be entered. This is what authenticates you to the session. Open a standard PowerShell as Administrator.

' $MSCred = Get-Credential '

' $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $MSCred -Authentication Basic -AllowRedirection '

' Import-PSSession $Session '

Set Execution Policy allows you to actually run power shell commands against your Office 365 account. Choose Yes when prompted.

' Set-ExecutionPolicy RemoteSigned '

You are now connected to your Office 365 session and ready to execute power shell commands.

Grant an admin access to all user mailboxes in Office 365

You will need to modify this command before entering it into office 365 power shell. In this case “AdministratorAccount@contoso.com” in the command below must be changed to the administrative account you want to use. Note that the full e-mail address must be used.

' Get-Mailbox -ResultSize unlimited -Filter {(RecipientTypeDetails -eq 'UserMailbox') -and (Alias -ne 'Admin')} | Add-MailboxPermission -User AdministratorAccount@contoso.com -AccessRights fullaccess -InheritanceType all '

Cut and past this command in power shell once connected to office 365. It will output email-addresses.csv to the C:\ drive.

Get-Mailbox -ResultSize Unlimited |Select-Object DisplayName,PrimarySmtpAddress, @{Name=“EmailAddresses”;Expression={$_.EmailAddresses |Where-Object {$_.PrefixString -ceq “smtp”} | ForEach-Object {$_.SmtpAddress}}} | Export-Csv c:\email-addresses.csv

Connecting Office 365 Azure Directory (do this before anything below)

To run commands against Azure directory you must install some software.

'Step One'

Microsoft Online Services Sign-in Assistant:

You will need this small piece of software installed to be able to connect to Office365. It is also necessary for running the Lync client on your desktop.

You can download it here: http://go.microsoft.com/fwlink/?LinkId=286152

'Step Two'

The next thing that you will need is access to the Office365 cmdlets so that you will have the commands that you need to manage your domain. You will need to download and install these files:

32 bit - http://go.microsoft.com/fwlink/p/?linkid=236298

64 bit - http://go.microsoft.com/fwlink/p/?linkid=236297

' Step 3 Scripting your connection'

Once you have finished the above you're ready to actually connect to your Office365 domain. Launch the Windows Azure Active Directory Module for Windows PowerShell. Here is the syntax that you will need to run:

'Import-Module MSOnline'

'$O365Cred = Get-Credential'

'$O365Session = New-PSSession –ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $O365Cred -Authentication Basic -AllowRedirection'

'Import-PSSession $O365Session -AllowClobber'

'Connect-MsolService –Credential $O365Cred'

By default Office 365 sets an annoying temporary password. Below is the process to reset users passwords in Bulk along with adjusting other password related settings.

This command basically has you exporting a .CSV file with a list of users, then running a command against the exported .CSV file.

First you must connect to the msol service to execute this command.

' Connect-MsolService '

Run the command to export the CSV to the root of C:.

' Get-MSOLUser | Select UserPrincipalName|Export-Csv c:\password.csv '

Edit the file to remove any users you don't want affected by the password change and save the file. For example the administrative account.

You will need to modify this command before entering it into office 365 power shell. In this case change “somestaticpassword” to the password you want for all users.

' Import-Csv c:\password.csv|%{Set-MsolUserPassword -userPrincipalName $_.UserPrincipalName -NewPassword somestaticpassword -ForceChangePassword $false} '

All Passwords should be changed.

Reset Different Passwords

This command will be used to reset each user to a different password from a CSV file.

Import-Csv c:\password.csv|%{Set-MsolUserPassword –userPrincipalName $_.upn -NewPassword $_.newpassword -ForceChangePassword $false}

To set the password of one user to never expire, run the following cmdlet by using the UPN or the user ID of the user:

'Set-MsolUser -UserPrincipalName <user ID> -PasswordNeverExpires $true'

To set the passwords to never expire for all the users in an organization, run the following cmdlet:

'Get-MSOLUser | Set-MsolUser -PasswordNeverExpires $true'

Get-Mailbox | Get-MailboxStatistics | Sort totalitemsize -desc | ft displayname, totalitemsize, itemcount

Or to export to CSV:

Get-Mailbox | Get-MailboxStatistics | Sort totalitemsize -desc | select-object displayname, totalitemsize, itemcount | Export-Csv -notype “C:\temp\UserMailboxSize.csv”

  • windows_wiki/powershell_office_365.txt
  • Last modified: 2019/05/25 23:50
  • (external edit)