11 PowerShell commands for Office 365 that are useful to know – InformTFB

11 PowerShell commands for Office 365 that are useful to know

11 PowerShell commands for Office 365 that are useful to know

Using PowerShell to manage Office 365 can make your work faster, more efficient, and easier. PowerShell provides access to information about the Office 365 environment that cannot be accessed through the Microsoft 365 administration center, and allows you to perform bulk operations with a single command. By integrating Office 365 products into a single interface, PowerShell also simplifies user access management and enhances cybersecurity.
This article explains the most useful PowerShell commands for Office 365 for system administrators. We’ve divided these commands into three categories — automation, reporting, and configuration-so you can quickly find what you need.

How PowerShell can help you work with Office 365

The Microsoft 365 admin center is great for regular users. Using this system, you can manage your Office 365 user accounts and licenses, as well as services such as Exchange Online, Teams, and SharePoint Online. You can also manage all of these components using PowerShell. Its use greatly simplifies automation and makes your work more efficient.
In particular, there are several key factorsthat make Office 365 easier to manage with PowerShell:

  • PowerShell for Office 365 shows additional information that you can’t see in the Microsoft 365 admin center.;
  • PowerShell allows you to configure features and settings that are not available in Office 365 Central administration;
  • If you use Office 365 for file sharing, PowerShell for Office 365 allows you to quickly check and manage user access to shared disks.;
  • You can easily perform bulk operations from the command line;
  • In PowerShell for Office 365, you can use cmdlets to filter data received from your Office 365 system. This gives you quick access to information about users and systems.;
  • You can also use it to automate the process of collecting data from Office 365 and uploading it to a CSV file.;
  • With the ability to quickly check user information, PowerShell is a powerful tool for monitoring and improving cybersecurity.

All of these features are extremely useful for system administrators. However, it should be noted that PowerShell helps extend the management capabilities of Office 365, and does not replace the Microsoft 365 administration center. Some tasks will be more efficient using Central administration, and Vice versa. some configuration procedures can only be performed using PowerShell commands.

Once you’ve mastered the basics of PowerShell, the system is almost infinitely extensible. There are dozens of PowerShell tools available which can simplify and speed up system administration, and use the command line to run scripts to automate frequent and time-consuming tasks.

Finally, try the PowerShell integrated scripting development environment (ISE) for all your PowerShell-related needs. This environment not only simplifies the creation of PowerShell scripts, but also improves interaction with the command-line interface.

PowerShell commands for office 365 automation

Instead of manually processing dozens of user accounts, you can use PowerShell to quickly collect, filter, and organize information about Office 365 users. Then, using the same command-line interface, you can perform bulk actions on the desired account.

The most useful PowerShell commands for office 365 automation:

1. Connect to the Office 365 app using PowerShell

Before you can start using PowerShell for Office 365, you need to download and install the Office 365 for Windows PowerShell module and connect it to your Office 365 instance.

Here’s how to do it:

  • Download and installThe sign-in assistant Microsoft Online Services for it professionals RTW.
  • Import the PowerShell Online Services module for Microsoft Azure Active Directory and Office 365 using the following commands in PowerShell:
    1. Install-Module -Name AzureAD 2. 3. Install-Module -Name MSOnline
  • Enter your Office 365 administrator credentials:
    $Cred = Get-Credential
    Now you need to create a PowerShell session on behalf of the remote user. You can do this with the following command:
    $O365 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Cred -Authentication Basic -AllowRedirection
  • Now import the session commands into your local Windows PowerShell session:
    Import-PSSession $O365
  • Finally, connect the session to all of your Office 365 services with this command:
    Connect-MsolService –Credential $O365
    This will connect PowerShell for Office 365 to your Office 365 instance and allow you to manage it.

2. Connect to Exchange Online and SharePoint Online using PowerShell

You can connect to Microsoft Exchange Online and Microsoft SharePoint Online to manage these services using PowerShell.

  • Connecting to Exchange Onlineis essentially the same as connecting to Office 365. Here are the relevant commands:
    1. $Cred = Get-Credential 2. 3. $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Cred -Authentication Basic –AllowRedirection
  • Connecting to SharePoint Online is a bit more complicated, and you’ll need to install additional software.
    First, install the SharePoint Online command console component.

    Then run the following command from PowerShell:
    1. $admin="Admin@enterprise.onmicrosoft.com" 2. 3. $orgname="enterprise" 4. 5. $userCred = Get-Credential -UserName $admin -Message "Укажите пароль." 6. 7. Connect-SPOService -Url https://$orgname-admin.sharepoint.com -Credential $userCred

3. List of available PowerShell cmdlets for Office 365

Cmdlets are the main type of PowerShell commands for Office 365, and you’ll use them most often. PowerShell for Office 365, like most command-line interfaces, lets you view a list of all available cmdlets for your system.

  • To get a list of all available cmdlets for MSOnline, run the following command:
    Get-Command -module MSOnline
  • You can also run the same command to see a list of all available cmdlets for Azure Active Directory by simply replacing the-module variable.:
    Get-Command -module AzureAD

4. A list of all Office 365 users

One of the most common uses of PowerShell is to get a list of all Office 365 users. In PowerShell, you can do this with just one cmdlet: Get-msoluser.
This cmdlet will show you all Office 365 users with a valid license and automatically get some basic information about each of them: the DisplayName, City, Department, and ObjectID parameters.

  • To do this, run the command:
    1. Get-MsolUser | Select DisplayName, City, Department, ObjectID
  • You can then see the number of accounts by running a similar command:
    1. Get-MsolAccountSku
  • And to get a list of available services, run this command:
    1. Get-MsolAccountSku | select -ExpandProperty ServiceStatus
  • Using standard command-line logic, you can extend these commands to filter the results you get. For example, you can group all users based on a location by running:
    1. Get-MsolUser | Select DisplayName, UsageLocation | Sort UsageLocation, DisplayName

5. Create a new user in Office 365 using PowerShell

You can use PowerShell to automate the process of creating new users on your system. To do this, use the New-MsolUser cmdlet:
To automate the user creation process, use this cmdlet with the appropriate variables:

1.	New-MsolUser -UserPrincipalName JSmith@enterprise.onmicrosoft.com -DisplayName "John Smith" -FirstName “John” -LastName “Smith”

After executing this command, PowerShell displays information about the user you created, including their temporary password and license status.

6. Change your office 365 password using PowerShell

One of the most common and most annoying tasks for system administrators is changing the user’s password. Ideally , use a password Manager for this, but PowerShell gives you the ability to automatically update passwords for individual users.

To do this, run the command:

1.	Set-MsolUserPassword -UserPrincipalName JSmith@netwrixqcspa.onmicrosoft.com -NewPassword P@SSw0rd!

You can also choose not to use the-NewPassword parameter, in which case the system will automatically generate a random password:

1.	Set-MsolUserPassword -UserPrincipalName JSmith@netwrixqcspa.onmicrosoft.com

Windows PowerShell commands for reporting

PowerShell for Office 365 is a great tool for creating reports. Using PowerShell cmdlets, You can quickly and easily access, sort, and match information about Office 365 users, as well as information about how they use the system.
Note that most of the report generation cmdlets are deprecated in January of 2018. Microsoft has replaced these cmdlets with the new Microsoft Graph reporting API. This reduced the ability of PowerShell to generate reports in Office 365, but all the old features are still available through the Office 365 security and compliance center.

However, when it comes to user and group reporting, PowerShell for Office 365 is still a key tool. Here are some of the most useful reports that you can use PowerShell For.

1. The licensing plans

PowerShell has an extremely useful cmdlet that lets you see a summary of your current licensing plans and available licenses for each plan. To use it, do the following:
To do this, run the command:

1.	Get-MsolAccountSku

As a result, you will receive a report containing several key information elements:

  • AccountSkuld-shows available licensing plans for your organization;
  • ActiveUnits — the number of licenses you purchased for a specific licensing plan;
  • WarningUnits — the number of non-renewed licenses in the licensing plan that expire at the end of the 30-day grace period;
  • ConsumedUnits — the number of licenses that you have assigned to users from a specific licensing plan.

You can also use additional syntax to get more information about your licenses or filter and sort the results. For more information on how to do this, see the Microsoft documentation on using PowerShell to generate reports.

2. User accounts

Another useful reporting cmdlet is Get-MsolUser, which returns a list of all Office 365 user accounts. Here’s how you can use this command:
Run the command:


1.	Get-MsolUser

You will see a complete list of user accounts with their respective names. You can also add a number of parameters to filter the displayed accounts. For example, to get a list of unlicensed users (users who have been added to Office 365 but have not yet received a license to use any of the services), run the following command:

1.	Get-MsolUser -UnlicensedUsersOnly

To further explore specific accounts, you can use the where cmdlet.
To combine two cmdlets, use the vertical line” |»: This means that PowerShell for Office 365 will take the results of the first command and send them to the next command. For example, if you want to display only those user accounts that do not have a specified usage location, you can use the following command:

1.	Get-MsolUser | Where {{$_.UsageLocation -eq $Null}}

By adding additional syntax after the vertical bar character”|”, you can specify reports and get lists of users with any combination of attributes.

3. The e-mail reports

PowerShell is also a powerful tool for checking email usage and users. In fact, it is one of the main system applications when it comes to reporting. Some useful email-related reports are listed below:

  • You can use PowerShell To get information about each mailbox on your system by using the following command:
    1. Get-mailbox | get-MailboxStatistics
  • You can also get a list of all mailboxes that have not been logged in for 30 days (or any other period you need that means you need to close these mailboxes). To do this, run the command:
    1. Get-Mailbox –RecipientType 'UserMailbox' | Get-MailboxStatistics | Sort-Object LastLogonTime | Where {{$_.LastLogonTime –lt ([DateTime]::Now).AddDays(-30) }} | Format-Table DisplayName, LastLogonTime
  • Another useful tool for ensuring cybersecurity is checking the activity of your mailboxesto mark those that send and receive the most mail. There is a special cmdlet for this task. Run the command:
    1. Get-MailTrafficTopReport

PowerShell cmdlets for configuring Office 365

PowerShell is also extremely useful for configuring the Office 365 environment. As we point out in our free video course on hidden Office 365 settings that can be unlocked using PowerShell, there are certain configuration options that are only available through the PowerShell interface.
The most useful and frequently used PowerShell configuration commands are those related to managing user groups and creating new SharePoint sites. These tasks often complicate the work of system administrators, and automating them can save a lot of time.

1. Configure the “hidden” settings using PowerShell for Office 365

As we mentioned earlier, some Office 365 configuration settings can only be accessed using PowerShell.
The most prominent example is the Skype for business configuration settings. The online administration center for this service contains several parameters that allow you to configure how it works for your organization. However, with PowerShell, you’ll get access to more customization options. For example, standard Skype conferences are configured so that:

  • anonymous users can automatically log in to each conference;
  • participants can keep a record of the conference;
  • all users in your organization can be assigned as speakers.

To change these default settings, you can use Powershell. Here is a command to disable all three of the above options:

1.	Set-CsMeetingConfiguration -AdmitAnonymousUsersByDefault $False -AllowConferenceRecording $False -DesignateAsPresenter "None"

If you want to reset the settings to their default values, use the following command:

1.	Set-CsMeetingConfiguration -AdmitAnonymousUsersByDefault $True -AllowConferenceRecording $True -DesignateAsPresenter "Company"

This is just one example of hidden settings that you can access using PowerShell. To learn more, visit our free online course.

2. Manage Office 365 group memberships using PowerShell

PowerShell has several cmdlets specifically designed to work with Office 365 groups. For example:
Run this command to view a list of all active groups in Office 365.

1.	Get-MsolGroup

This command will also provide you with a hexadecimal identifier for each group that you will need to manage your membership.
To add and remove group members, you will also need the hexadecimal ID of their accounts, which can be obtained using the command:

1.	Get-MsolUser | Select ObjectID.

You can then run the appropriate cmdlet to add or remove users from specific groups. To add users, run the following command, replacing the hexadecimal identifiers specified in the example with identifiers that relate to your group and the desired user:

1.	Add-MsolGroupMember -GroupObjectId 5b61d9e1-a13f-4a2d-b5ba-773cebc08eec -GroupMemberObjectId a56cae92-a8b9-

To remove users from groups, you can run the same command, but with the first part changed:

1.	Remove-MsolGroupMember

How Varonis interacts with PowerShell

Varonis complements the use of PowerShell in several ways.

Monitoring

Varonis monitors and verifies Office 365 activity (including configuration changes) and PowerShell commands. This allows you to monitor any changes that administrators or attackers make using PowerShell.
Administrators must make changes to Office 365 configurations or permissions with a valid change request. This additional level of verification ensures smooth operation of processes and procedures.
Attackers are trying to use PowerShell To disable security measures or increase account permissions. Varonis catches these changes and tracks any other actions that attackers take in Office 365.

Get alerts and take action

Varonis allows users to run PowerShell scripts when they receive alerts.
The most common use of this feature is to automatically respond to ransomware attacks. The ransomware threat model invokes a script that deactivates the user’s account and shuts down all the computers they were logged on to to stop the attack.

Conclusion

PowerShell is an effective tool for working with Office 365. It allows you to quickly access information from the system, create detailed reports, and perform mass actions. You can also use it to access certain features of Office 365 that are not available in any other way.

Valery Radokhleb
Valery Radokhleb
Web developer, designer

Leave a Reply

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