Call a script inside another script

Hi everyone,

I was wondering if someone could help me,

I have two scripts,
one named " getClient_CountryCode.ps1"
and the other one named " Get_FolderID.ps1".

I want to call inside the second one, the first code and save it in a variable. I did: $ClientCountryCode=invoke-expression -Command .\getClient_CountryCode.ps1
Write-host $ClientCountryCode but there is no result.
I want to mention that there is a return.response.Id in the first script. I want this response to use it in my second script.
Anyone could give it a hand please?

Xhensila,
Welcome to the forum. :wave:t4:

This would be enough actually:

$ClientCountryCode = & .\getClient_CountryCode.ps1

A disadvantage is that you will get everything in your variable what’s outputted by the other script. So you should make sure that it does not output something you dont want in your variable. :wink:

Another option would be to run the second script in the same scope like the first one. This way you could even use the variables from the other script if you need. :wink:

$ClientCountryCode = & . .\getClient_CountryCode.ps1

Notice the additional dot in front of the path to the script you want to run. This way you could use the variables from the other script directly.

BTW: When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink: