# Office 365 – Using dynamic variable as prefix for Office 365 commands

One of the nice features of an Import-PSSession is Prefix parameter. If you have never used one let me try to…

blog

29 Oct 2018 pl

![Office 365 – Using dynamic variable as prefix for Office 365 commands](/wp-content/uploads/2018/10/img_5bd62f27ae36b.png)

One of the nice features of an **Import-PSSession** is **Prefix** parameter. If you have never used one let me try to show you an example that should clear things up for you. Let's assume You want to get mailbox from **Office 365** and **Exchange On-Premises** during the same session. Usually, you would do something like

```powershell
# Connect to Exchange Online
$Mailbox2 = Get-Mailbox -Identity 'MyMailbox1'
# Disconnect from Exchange Online
# Connect to Exchange On-Premises
$Mailbox2 = Get-Mailbox -Identity 'MyMailbox2'
# Disconnect from Exchange On-Premises
```

While this code would most likely work it's pretty inefficient and in more complicated scenarios not really useful. That's why **Import-PSSession** has a parameter called **Prefix**. This gives you a way to connect to **Office 365** and** Exchange Online** at the same time!

```powershell
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session -Prefix O365

# Get-Mailbox from Exchange On-Premises
Get-Mailbox -Identity 'MyMailbox1'

# Get-Mailbox from Exchange Online
Get-O365Mailbox -Identity 'MyMailbox2'
```

Isn't that cool? It is. But this is pretty common so there's probably 50 pages with information on how to use this. What I actually wanted to write about is the ability to dynamically define that prefix for a command.

### 💡 So what about that Prefix?

What if one wanted to have a function that you can allow the user to define prefix?

```powershell
function Connect-WinExchange {
    [CmdletBinding()]
    param(
        [string] $SessionName = 'Exchange',
        [string] $ConnectionURI = 'http://ex2013x3.ad.evotec.xyz/Powershell', # https://outlook.office365.com/powershell-liveid/
        [ValidateSet("Basic", "Kerberos")][String] $Authentication = 'Kerberos',
        [string] $Username,
        [string] $Password,
        [switch] $AsSecure,
        [switch] $FromFile,
        [string] $Prefix,
        [switch] $Output
    )
   # Some Code
    if ($Prefix) {
        #Write-Verbose "Prefix used $Prefix"
        Import-Module (Import-PSSession -Session $Session -AllowClobber -DisableNameChecking -Prefix $Prefix -Verbose:$false) -Global -Prefix $Prefix
    } else {
        #Write-Verbose "Prefix used - None"
        Import-Module (Import-PSSession -Session $Session -AllowClobber -DisableNameChecking -Verbose:$false) -Global
    }
}
```

Now notice how **Prefix** in the code above is available as a parameter and how I had to use **Import-Module** to be able to use [Import-PSSession in a separate module](https://evotec.xyz/office-365-using-import-pssession-from-separate-module/). Because of that Prefix has to be also defined for the Import-Module. Otherwise, **Prefix** within Import-Module gets lost, and all commands work as if there was no **prefix** at all. But back to the topic. Now that we passed a **Prefix **to **Connect-****WinExchange** we can try to use it in code.

```powershell
# Connect to Exchange and set dynamic prefix
Connect-WinExchange -Prefix 'Something'

# After we're connected we now needs to use
Get-SomethingMailbox
```

Is this a problem? No, not really. But what if there is another function taking this as a parameter? Things get complicated.

```powershell
function Get-WinExchangeMailbox {
    [CmdletBinding()]
    param(
        [string] $UserName,
        [string] $Prefix
    )
   # Normally you would do it like
   Get-SomeMailbox

   # But how to use prefix?
   Get-$PrefixMailbox

   # Or maybe?
   Get-$($Prefix)Mailbox
}
```

Well, the code above won't work. I did try it. If only it were that simple right?

### 💡 I want my answer now

The only way that worked for me (maybe not the only one out there thou) was to use it with **Call operator**

```powershell
& "get-$($prefix)mailbox"
```

[!\[Prefix\](/wp-content/uploads/2018/10/img_5bd63cb12d785.png)](/wp-content/uploads/2018/10/img_5bd63cb12d785.png)

**Call operator** allows you to execute string (among other things) which is what I wanted in this case.

## Powiązane projekty

Projekty powiązane z tagami, kategoriami i treścią tego wpisu.

[Wszystkie projekty](/projekty/)

Trwa dopasowywanie projektów...

[Powrót do bloga](/blog/)

## Powiązane wpisy

- [Check an API write after a PowerShell timeout](/blog/verify-uncertain-api-outcomes-powershell/)
- [Inspect and convert mixed Office archives with PSWriteOffice](/blog/inspect-and-convert-mixed-office-archives-pswriteoffice/)
- [Compose PDF reports and automate page workflows with PSWriteOffice](/blog/automate-pdf-reports-and-page-workflows-pswriteoffice/)

<!-- PowerForge markdown artifact sha256:C3D1338B01178F0604E7921325E5D8EB20F2051B8FC0334E4F7EBA52D390EBB9 -->
