Calling powershell script and keeping obtained variable in the 'main' script

I have been using PowerShell to automate some administrative functions.  A lot of the scripts contain common code and I would like to put this common code into its own separate script (process) The issue I am having, which is understandable, is that variable information in a 'called script' is not available to the main script.

Example ... Main script (Main.ps1)

# This script checks to see if Exchange command are loaded and if not loads them into PowerShell
Invoke-Expression .\LoadExchange.ps1

(This seems to work just fine!)

I have another script that prompts for a Mailbox name (assumed SamAccountName/Alias) is entered

Invoke-Expression .\ValidateMailbox.ps1

Here is ValidateMailbox.ps1
    $Temp = Read-Host 'Please enter the user logon name' -ErrorAction SilentlyContinue
    $Mailbox = Get-Mailbox $Temp
    If ($Error[0])
        $Logon = "error"
        $Logon = "good"
While ($Logon -eq "error")

When I run these commands inside the 'main' script everything works fine. If I call the ValidateMailbox.ps1; I am prompted to enter data however the information stored in $Mailbox is not available ... I know this doesn't extend outside the scope of the script from which is run.  

I have found lots of examples of passing parameters TO a script but can't seem to find a solution where a called script passes $variables back to the 'Main' script.
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

QlemoConnect With a Mentor DeveloperCommented:
The combination of scrope and variable name has to be unique for unique variables. $global:a is something different than $global:b. $script:a and $global:a are different.

More, since any change on a variable without scope implies it is the var in the nearest scope, but that does not apply to reading vars, you get the effect that you can see a global var value, but not change - changing is creating a local var. If you use the scope modifier, it is clear what you create and get.
Example (this are, of course, in fact two scripts):
$a = 1
  # --- now in subscript.ps1:
  $a = $a +1
  write-host $a
  # --- again in main script
write-host $a

Open in new window

will output 2, then 1.

Re dot-sourcing: There is a dot, then a space, then the scriptname (with path). You need to write
. c:\Scripts\validatemailbox.ps1
# or
. 'c:\Scripts\validatemailbox.ps1'

Open in new window

If you run a script dot-sourced, it will be executed in the same scope.
. .\ValidateMailbox.ps1

Open in new window

Note the "dot space" syntax.
More complex, but you can also specify the scope of specific variables.
Example syntax would be $global:mailbox = Get-Mailbox $Temp

I'd suggest reading the "about_scopes" PS help topic.
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

winsystemsAuthor Commented:
I tried to get the "dot-sourced" to work but couldn't ... mainly because I was using the full path name including "C:"receiving the message of "The term '.c:\Scripts\validatemailbox.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program".  It is amazing how such a little thing can cause frustration for a beginner.

After doing some further reading about scopes; I have another question as they pertain to the "Scope-Modifier" in the examples below:
The syntax for a scope modifier in a variable is:
        $[<scope-modifier>]:<name> = <value>
The syntax for a scope modifier in a function is:
        function [<scope-modifier>]:<name> {<function-body>}

Reference: about_Scopes

The scope modifier seems to be specific name like “Global”, “Script” or “Private”, correct? Assuming I understand this correctly there can be more than one $global:<variable_name> used in one or more ‘child scripts’ with variable information available to the ‘Main.ps1’ script as long as the <variable_name> for each child script is unique, correct?  

I am thinking of re-writing my administrative scripts to be a series of scripts calling other scripts other than the several hundred lines that they currently are as many of the processes we run are similar such as validation and error checking and while dot sourced is simple; scopes might provide some programing advantage

Thanks to all for assistance
footechConnect With a Mentor Commented:
As Qlemo noted, don't forget the space after the initial dot.  It's required whether you use the full path or not.

Personally, I've only ever used the "global" and "script" scope modifiers (my needs have been pretty simple).  Yes you can have as many variables as you want specified with the modifier.  The names don't necessarily have to be unique, just be aware of when it's OK to update the contents of a variable.  It may be less error prone to make them all unique, but just like in a single script with only one scope it's OK to reuse a variable name if you don't need the old contents anymore.
winsystemsAuthor Commented:
Thanks all for the helpful information.
All Courses

From novice to tech pro — start learning today.