# How to make Exchange Online exported functions visible outside function

**URL:** <https://forums.powershell.org/t/how-to-make-exchange-online-exported-functions-visible-outside-function/9331>\
**Category:** PowerShell Help\
**Created:** [September 11, 2017, 10:25am UTC](https://forums.powershell.org/t/how-to-make-exchange-online-exported-functions-visible-outside-function/9331 "2017-09-11T10:25:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![miracles1315](https://avatars.discourse-cdn.com/v4/letter/m/53a042/32.png) [@miracles1315](https://forums.powershell.org/u/miracles1315)\
**Post date:** [September 11, 2017, 10:25am UTC](https://forums.powershell.org/t/how-to-make-exchange-online-exported-functions-visible-outside-function/9331/1 "2017-09-11T10:25:48Z")

</div>

I have a scope issue that I found the answer to, but I thought I’d see if my fix is the only way to go or if there is a better/correct one.

Don Jones: I know in your Learn PowerShell Toolmaking In A Month of Lunches book, you say “Typing $global means you’re doing it wrong, wrong, wrong.”, thus I thought I’d bring this up here. 🙂

I’ve got a function, inside of a module, that I’m using to create a PS session to Office 365 Exchange Online. It works great, except that none of the commands that are exported by the service (Ex: Get-Mailbox) are visible at the console/ISE level for me to be able to run. If I manually run the function code in the PowerShell console/ISE, I can see, and run, the exported commands. So, I know the problem is that the exported Exchange Online commands are only “visible” inside the scope of the function. I need them visible at the PowerShell console/ISE level so I can run them after the function has made the connect to the Exchange Online service.

Here is a very slimmed down version of my script that has the scope issue.

```
Function Connect-O365ExchangeOnline
{
  $Credential = Get-Credential
  $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Credential -Authentication Basic -AllowRedirection
  Import-PSSession $Session
}
```

Here is my fix:

```
Function Connect-O365ExchangeOnline
{
  $Credential = Get-Credential
  $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Credential -Authentication Basic -AllowRedirection
  $Global:ExportedCommands = Import-PSSession $Session
  Return $ExportedCommands
}
```

Is there a better/correct way to make this work? If not…Don Jones, please forgive me…lol 🙂

---

<div class="post-metadata">

**Author:** ![maartenr](https://avatars.discourse-cdn.com/v4/letter/m/2acd7d/32.png) [@maartenr](https://forums.powershell.org/u/maartenr)\
**Post date:** [September 11, 2017, 12:54pm UTC](https://forums.powershell.org/t/how-to-make-exchange-online-exported-functions-visible-outside-function/9331/2 "2017-09-11T12:54:21Z")

</div>

Hi Kevyn,

I don’t know if there is a better way, but when you look at this module I see a lot of global variables.

> **[O365\_Logon 1.1](https://www.powershellgallery.com/packages/O365_Logon/1.1)**
>
> O365 logon cmdlets to assist IT administrators. In this module, there are several cmdlets that simplify the process of logging onto various O365 components.

---

<div class="post-metadata">

**Author:** ![miracles1315](https://avatars.discourse-cdn.com/v4/letter/m/53a042/32.png) [@miracles1315](https://forums.powershell.org/u/miracles1315)\
**Post date:** [September 11, 2017, 1:01pm UTC](https://forums.powershell.org/t/how-to-make-exchange-online-exported-functions-visible-outside-function/9331/3 "2017-09-11T13:01:14Z")

</div>

Interesting. Thanks.

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [September 11, 2017, 1:24pm UTC](https://forums.powershell.org/t/how-to-make-exchange-online-exported-functions-visible-outside-function/9331/4 "2017-09-11T13:24:02Z")

</div>

Yyyyyeah, so, you’re really running an edge case, where you want a function to be a “helper” and to genuinely modify the global scope you’re in. Using the $global modifier in that case is less of a sin. _Personally_ I would probably have written that as a script, not a function, and just dot-sourced it - but you’re getting the same effect.

This isn’t using $global to pass data around; your goal is to legit modify the global scope, so you’re not really in the sin zone.

---

<div class="post-metadata">

**Author:** ![miracles1315](https://avatars.discourse-cdn.com/v4/letter/m/53a042/32.png) [@miracles1315](https://forums.powershell.org/u/miracles1315)\
**Post date:** [September 11, 2017, 1:29pm UTC](https://forums.powershell.org/t/how-to-make-exchange-online-exported-functions-visible-outside-function/9331/5 "2017-09-11T13:29:52Z")

</div>

Thanks, Don. Haven’t yet gotten to the helper functions, in your book. I may take a sneak peak ahead. 😉

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:37pm UTC](https://forums.powershell.org/t/how-to-make-exchange-online-exported-functions-visible-outside-function/9331/6 "2024-05-16T20:37:38Z")

</div>

Closed on May 16, 2024, 8:37pm UTC


