PowerShell

Office 365 – Using Import-PSSession from separate module

Recently I've been working on a little code allowing me to connect to Office 365. It's not a really big deal, and there's a lot of options around. I wanted something of my own and something that outputs stuff I want it to output. All was going fine until I decided to move the same code that was working correctly into a separate module. Even thou commands Connect-WinExchange or Connect-WinAzureAD were executing successfully none of the commands from those sessions worked. No Get-Mailbox, no Get-MailContact. It turns out that being in another module actually puts a scope on it and therefore it's not provided when used outside of that module. I've considered two workarounds for this.

💡 Workaround One

Use Import-PSSession in your main module and keep all other code in your “connectivity” module.

Import-PSSession -Session $Session -AllowClobber -DisableNameChecking -Prefix $Prefix -Verbose:$false

And this basically works as expected. The only thing is you can't have Connect-WinExchange as a standalone command. You need to use it just to build connection.

$Session = Connect-WinExchange -Credentials $Credentials
Import-PSSession -Session $Session -AllowClobber -DisableNameChecking -Prefix $Prefix -Verbose:$false

If you can live with it.. it's good solution.

💡 Workaround Two

Other workaround is to use Import-Module. While it doesn't come as solution at first sight you can actually do something like this

Import-Module (Import-PSSession -Session $Session -AllowClobber -DisableNameChecking -Verbose:$false) -Global

Therefore your function would look like this (more or less)

function Connect-WinExchange {
    [CmdletBinding()]
    param(
        [string] $SessionName = 'Exchange',
    )
    # Your Code Goes Here
    Import-Module (Import-PSSession -Session $Session -AllowClobber -DisableNameChecking -Verbose:$false) -Global
}

This post was last modified on June 7, 2025 11:37

Przemyslaw Klys

System Architect with over 14 years of experience in the IT field. Skilled, among others, in Active Directory, Microsoft Exchange and Office 365. Profoundly interested in PowerShell. Software geek.

Share
Published by
Przemyslaw Klys

Recent Posts

Supercharging Your Network Diagnostics with Globalping for NET

Ever wondered how to run network diagnostics like Ping, Traceroute, or DNS queries from probes…

5 days ago

Automating Network Diagnostics with Globalping PowerShell Module

Are you tired of manually running network diagnostics like Ping, Traceroute, or DNS queries? The…

5 days ago

Enhanced Dashboards with PSWriteHTML – Introducing InfoCards and Density Options

Discover new features in the PSWriteHTML PowerShell module – including New-HTMLInfoCard, improved layout controls with…

2 weeks ago

Mastering Active Directory Hygiene: Automating SIDHistory Cleanup with CleanupMonster

Security Identifier (SID) History is a useful mechanism in Active Directory (AD) migrations. It allows…

2 weeks ago

Upgrade Azure Active Directory Connect fails with unexpected error

Today, I made the decision to upgrade my test environment and update the version of…

2 weeks ago

Mastering Active Directory Hygiene: Automating Stale Computer Cleanup with CleanupMonster

Have you ever looked at your Active Directory and wondered, "Why do I still have…

2 weeks ago