How do I pass variable(s) from one script to another

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 6 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
MikePro85
Posts: 3
Last visit: Fri May 26, 2023 10:43 am

How do I pass variable(s) from one script to another

Post by MikePro85 »

I'm trying to find a way to pass variables from one ps script to another. In cmd there was the call command, where an other script (batch file) could be called and the called batch file would use the variables from the initial script. It could write variables as well, that could be used later on again by the main script.

Is it possible, to do this in powershell scripting as well? How can I pass variables through (both ways) between a parent.ps1 and a child.ps1?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: How do I pass variable(s) from one script to another

Post by jvierra »

All variables visible to the parent script are also visible in the child. They are "in scope".

You can explicitly pass arguments to parameters of a called script.

Start by reading a book or taking an online tutorial on PowerShell and all of this will become clear. You will not be able to guess your way to understanding scripting.

Also see:

help about_variables
help about_scripts
This topic is 6 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked