How to configure Microsoft 365 security settings using PowerShell

How to configure Microsoft 365 security settings using PowerShell

As more organizations move to the cloud, it is becoming increasingly important to ensure the security of their Microsoft 365 environment. Microsoft provides a range of security settings that can be configured to enhance the security of your organization's data and protect against potential security threats. In this blog, we will explore how to configure Microsoft 365 security settings using PowerShell.


Configuring Microsoft 365 security settings using PowerShell can help you enhance the security of your organization's data and protect against potential security threats. In this step-by-step guide, we will explore how to configure Microsoft 365 security settings using PowerShell.


Step 1: Install Required Modules


To get started with Microsoft 365 PowerShell, you need to install the following modules:

  • Microsoft Online Services Sign-In Assistant
  • Azure Active Directory PowerShell for Graph


You can download these modules from the Microsoft website or install them using the PowerShell Gallery.


To install the Microsoft Online Services Sign-In Assistant, run the following command in PowerShell:


  1. Install-Module-Name MSOnline


To install the Azure Active Directory PowerShell for Graph, run the following command in PowerShell:


  1. Install-Module-Name AzureAD


Step 2: Connect to your Microsoft 365 Tenant


To connect to your Microsoft 365 tenant, run the following command in PowerShell:


  1. Connect-MsolService


This will prompt you to enter your Microsoft 365 admin credentials. Once you have authenticated, you can start using PowerShell to manage your Microsoft 365 environment.


Step 3: Configure Microsoft 365 Security Settings


There are many security settings that you can configure in Microsoft 365 using PowerShell. Here are some examples:


1. Configure Password Policies

You can configure password policies in Microsoft 365 to ensure that users create strong passwords and change them regularly. To configure password policies using PowerShell, run the following command:


  1. Set-MsolPasswordPolicy -ValidityPeriod90 -NotificationDays14 -DomainName yourdomain.com -MaxPasswordAge60 -MinPasswordLength8 -MinPasswordComplexCharacters3 -PasswordHistoryCount5


This command sets a password validity period of 90 days, a notification period of 14 days before password expiration, a maximum password age of 60 days, a minimum password length of 8 characters, a requirement for at least 3 complex characters, and a password history count of 5.


2. Configure Multi-Factor Authentication (MFA)

You can configure multi-factor authentication (MFA) in Microsoft 365 to require users to provide additional authentication factors when accessing sensitive information. To enable MFA using PowerShell, run the following command:


  1. Set-MsolUser -UserPrincipalName user1@yourdomain.com -StrongAuthenticationRequirements @(@{Type="MFA";Method="OneWaySMS";})


This command enables MFA for the user user1@yourdomain.com using one-way SMS as the authentication method. You can also use other authentication methods, such as phone call, mobile app, or hardware token.


3. Configure Conditional Access Policies

You can configure conditional access policies in Microsoft 365 to control access to your organization's resources based on specific conditions, such as device type, location, or risk level. To configure conditional access policies using PowerShell, run the following command:


  1. New-AzureADPolicy -Definition @('{"AuthMethods":{"Include":[{"Type":"Device"}]},"Applications":{"Include":[{"ApplicationId":"00000002-0000-0ff1-ce00-000000000000"}]},"ConditionalAccessLocations":[{"Location":"IPRanges","IPRanges":["1.2.3.4/32"]}],"Conditions":{"IncludeConditions":[{"Type":"RiskLevel","Value":"High"}]},"DisplayName":"Restrict access to Exchange Online from risky devices","State":"enabled"}') -DisplayName "Exchange Online: High Risk"


This command creates a conditional access policy that restricts access to Exchange Online from devices with a high risk level, located in the IP range 1.2.3.4/32.



Conclusion


PowerShell can help you enhance the security of your organization's data and protect against potential security threats. By following the steps outlined in this guide, you can configure various security settings in Microsoft 365 using PowerShell.

Remember that security is an ongoing process, and it's important to regularly review and update your security settings to stay protected against new and emerging threats. PowerShell can be a powerful tool in this regard, allowing you to automate security tasks and ensure that your organization's data remains secure.


Finally, keep in mind that Microsoft provides many other security features and tools that you can use to enhance the security of your Microsoft 365 environment. Be sure to explore these features and tools to ensure that your organization is fully protected against potential security threats.


    • Related Articles

    • Generate an Activity Report for Microsoft 365 Groups and Teams

      Introduction The activity reports available for Microsoft 365 groups and Teams can be beneficial for administrators in an organization. Microsoft 365 teams group activity reports provide insight into group activities, group workloads, group counts, ...
    • How to find unused Exchange Online mailboxes

      What are unused Exchange Online mailboxes and how to identify them? Unused Exchange Online mailboxes are user mailboxes which are currently not being used by their users. There are 3 ways in which we can identify if a mailbox is unused or not. They ...
    • How to create and manage Microsoft 365 groups with PowerShell

      Introduction: Microsoft 365 Groups is a collaboration feature that allows users to work together and share resources such as calendars, files, and email messages. Microsoft 365 Groups can be created and managed using the Microsoft 365 admin center, ...
    • Turn off User or Computer settings of a GPO

      Finding a way to disable either the user or computer settings of a GPO alone without disabling the entire GPO is easier said than done. However, the PowerShell script like the below can help to do so easily.   Provide the Fully Qualified Domain Name ...
    • Managing Microsoft 365 Compliance and Retention Policies with PowerShell

      Microsoft 365 provides a range of compliance and retention features that enable organizations to meet their legal, regulatory, and business requirements. These features include compliance policies, retention policies, and retention labels. PowerShell ...