PowerShell Basics for Office 365 Administration (Episode 1)
What is PowerShell?
PowerShell is a
command-line shell and a scripting language all in one. It is designed for system
administration, task automation, and configuration management, particularly
in Windows and cloud environments like Microsoft 365 (Office 365) and Azure.
How to Install PowerShell?
PowerShell is
pre-installed on Windows systems like Windows PowerShell. However, if you use a
computer that runs Linux, macOS, or an older version of Windows, you need to
install it. On macOS and Linux, PowerShell can be installed via package
managers such as Homebrew (macOS) or apt/yum (Linux). See more: Install PowerShell on Windows, Linux, and macOS
Comparison
between PowerShell and PowerShell ISE:
Feature |
PowerShell
(Console) |
PowerShell ISE
(Integrated Scripting Environment) |
Interface |
Command-line
interface (CLI) |
Graphical User
Interface (GUI) |
Performance |
Lightweight and
fast |
Heavier, slower
compared to console |
Script Editing |
No built-in editor |
Built-in editor
with syntax highlighting |
Debugging |
Basic |
Advanced debugging
tools (breakpoints, step-through) |
Auto-completion |
Basic tab
completion |
IntelliSense with
suggestions and parameter info |
Multiline Editing |
Limited |
Full multiline
scripting support |
Tooltips and Help |
Limited |
Rich tooltips and
parameter help |
Customization |
Highly
customizable (via profiles, themes) |
Limited
customization |
Cross-platform
Support |
Yes (via
PowerShell Core - pwsh) |
No (Windows only) |
Development Status |
Actively developed |
Deprecated (no
longer under active development by Microsoft) |
Use Case |
Quick tasks,
automation, remote sessions |
Script writing,
testing, and debugging |
Note: Microsoft no longer
updates the PowerShell ISE. The ISE only works with Windows PowerShell 5.1.
Visual Studio Code
(VS Code) with the PowerShell
extension works with both versions of PowerShell.
Determine Version of PowerShell:
To determine your
PowerShell version, just run this command in any PowerShell window (Windows
PowerShell, PowerShell ISE, or PowerShell 7+):
$PSVersionTable
Note: PowerShell version
7 isn't a replacement for Windows PowerShell 5.1; it installs side-by-side with
Windows PowerShell. Windows PowerShell version 5.1 and PowerShell version 7 are
two different products.
Understanding
PowerShell Execution Policies:
PowerShell Execution
Policy is a safety setting in PowerShell that determines whether you’re
allowed to run scripts, and under what conditions. By default, Windows will
not allow you to run these scripts by just double-clicking the file. This is
because malicious (or poorly written) scripts can cause a lot of accidental
damage to your system.
It’s not a security
boundary, but more of a safety mechanism to prevent accidental script
execution.
You can set an
execution policy for the local computer, current user, or a PowerShell session.
You can also set execution policies for users and computers with Group Policy
(GPO).
Types of Execution Policies:
- Restricted- Default. No scripts allowed. Interactive commands only.
- AllSigned- Only signed scripts (with a trusted certificate) can run.
- RemoteSigned- Local scripts can run. Internet/downloaded scripts must be signed.
- Unrestricted- All scripts can run. You’ll get a warning for downloaded ones.
- Bypass- No restrictions. Useful for automation, but risky if not controlled.
To see your current
setting, run:
Get-ExecutionPolicy
Or, to see all
levels (user/machine/etc.):
Get-ExecutionPolicy
-List
Change Execution
Policy (Recommended):
Set-ExecutionPolicy
-Scope CurrentUser -ExecutionPolicy RemoteSigned
Before you change
the execution policy, read the about_Execution_Policies help article to understand
the security implications.
Understanding Office 365 PowerShell Cmdlets, Functions, Modules, and Scripts
Cmdlets (pronounced command-lets) are lightweight PowerShell commands that perform specific administrative tasks. They follow a Verb-Noun naming convention (e.g., Get-, Set-, New-) and are designed for managing Office 365 services such as Exchange Online, SharePoint Online, Microsoft Teams, and Azure AD.
Examples:
- Get-EXOMailbox – Retrieves mailbox details in Exchange Online.
- Set-MgUser – Modifies a Microsoft Entra (Azure AD) user account.
- Connect-SPOService – Establishes a connection to SharePoint Online.
Functions are reusable blocks of PowerShell code that can accept parameters and return values. They help automate repetitive Office 365 tasks, such as user provisioning, license assignments, or report generation.
Example:
function
Get-InactiveUsers {
Get-MgUser -All | Where-Object {
$_.LastLoginDateTime -lt (Get-Date).AddDays(-90) }
}
Modules are packages that contain related cmdlets, functions, and scripts for managing specific Office 365 services. Microsoft provides official modules to interact with different workloads.
Key Office 365 Modules:
- Exchange Online (ExchangeOnlineManagement) – Manages mailboxes and distribution groups.
- Microsoft Graph (Microsoft.Graph) – Controls users, groups, and permissions via Graph API.
- SharePoint Online (PnP.PowerShell) – Handles SharePoint and OneDrive automation.
Usage:
Install-Module Microsoft.Graph -ForceImport-Module Microsoft.Graph.Users
Scripts (.ps1 files) are collections of PowerShell commands that automate complex Office 365 workflows, such as bulk user management, security policy enforcement, or audit log exports.
Example Script:
Get-MgUser -All | Select-Object DisplayName, UserPrincipalName | Export-Csv -Path "C:\Users\AllUsers.csv" -NoTypeInformation
Write-Host "User list exported successfully!"
Execution:
- Run
scripts via .\script.ps1 in PowerShell.
- Schedule
them in Task Scheduler for automated runs.
Get-Help in Office
365
The Get-Help cmdlet
is a valuable resource when managing Office 365 environments through
PowerShell. It provides detailed information about various cmdlets and their
usage.
Example:
To get help on the
Connect-ExchangeOnline cmdlet, which is used to establish a connection to
Exchange Online in Office 365:
Get-Help
Connect-ExchangeOnline
This command will show the syntax, parameters, and usage examples for connecting to Exchange Online.
You can also get
detailed help on a specific parameter by using the -Parameter flag. For
instance, to get help on the PropertySets parameter for the Get-EXOMailbox
cmdlet:
Get-Help
Get-EXOMailbox -Parameter PropertySets
Additionally, you can view online documentation directly by adding the -Online flag:
Get-Help
Connect-ExchangeOnline -Online
This will open the
official Microsoft documentation for this cmdlet in your browser.
Get-Command in PowerShell
The Get-Command
cmdlet allows you to discover all available commands for managing your Office
365 environment, including those for Exchange Online, Teams, SharePoint, and
more.
Example 1:
To list all cmdlets
related to Exchange Online management, use the following command:
Get-Command -Module
ExchangeOnlineManagement
This will return all available cmdlets within the ExchangeOnlineManagement module, which is commonly used for Exchange Online tasks such as managing mailboxes and mail flow.
Example 2:
To search for a
specific type of command, such as all cmdlets that manage users, you can filter
by the -Type parameter:
Get-Command -Type
Cmdlet | Where-Object { $_.Name -like "*User*" }
This command will list all cmdlets related to managing users in Office 365, such as Get-MsolUser, Set-MsolUser, etc.
Comments
Post a Comment