Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9023349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:44:24+00:00 2026-06-16T05:44:24+00:00

I have a PowerShell script/function that works great when I use it in my

  • 0

I have a PowerShell script/function that works great when I use it in my PowerShell profile or manually copy/paste the function in the PowerShell window.

I’m trying to make the function accessible to other members of my team as a module. I want to have the module stored in a central place so we can all add it to our PSModulePath.

Here is a copy of the basic function:

Function Connect-O365{
    $o365cred = Get-Credential username@domain.onmicrosoft.com
    $session365 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $o365cred -Authentication Basic -AllowRedirection
    Import-PSSession $session365 -AllowClobber
}

If I save this function in my PowerShell profile it works fine. I can dot source a *.ps1 script with this function in it and it works as well.

The issue is when I save the function as a *.psm1 PowerShell script module. The function runs fine but none of the exported commands from the Import-PSSession are available. I think this may have something to do with the module scope.

I’m looking for suggestions on how to get around this.

EDIT

When I create the following module and run Connect-O365 the imported cmdlets will not be available.

$scriptblock = {
    Function Connect-O365 {
        $o365cred = Get-Credential username@domain.onmicrosoft.com
        $session365 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://ps.outlook.com/powershell/" -Credential $o365cred -Authentication Basic -AllowRedirection
        Import-PSSession $session365 -AllowClobber
    }
}

New-Module -Name "Office 365" -ScriptBlock $scriptblock

When I import the next module without the Connect-O365 function the imported cmdlets are available.

$scriptblock = {
    $o365cred = Get-Credential username@domain.onmicrosoft.com
    $session365 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://ps.outlook.com/powershell/" -Credential $o365cred -Authentication Basic -AllowRedirection
    Import-PSSession $session365 -AllowClobber
}

New-Module -Name "Office 365" -ScriptBlock $scriptblock

This appears to be a scope issue of some sort, just not sure how to get around it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:44:25+00:00Added an answer on June 16, 2026 at 5:44 am

    With some assistance from TechNet I was able to modify the script module so it worked the way I expected.

    function Connect-O365 {
        $o365cred = Get-Credential username@domain.onmicrosoft.com
        $session365 = New-PSSession `
                        -ConfigurationName Microsoft.Exchange `
                        -ConnectionUri "https://ps.outlook.com/powershell/" `
                        -Credential $o365cred `
                        -Authentication Basic `
                        -AllowRedirection 
        Import-Module (Import-PSSession $session365 -AllowClobber) -Global
    }
    

    TechNet Post

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function in a powershell script that is supposed to untar my
I have function in a powershell 2.0 script that I am launching as a
I have a test powershell V2 script that looks like this: function test_args() {
I have a powershell script that works fine if I run it from an
I have a powershell script that will generate an email for users whose password
I have a complex Powershell script that gets run as part of a SQL
I have developed a Powershell script that needs to be run automatically daily. This
I trying to write a PowerShell script (learning at the same time). I have
I have a powershell script that runs a console .exe application. The console application
I have a PowerShell script that configures web site and web application settings in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.