How to use a global variables file in powershell

Mohammed Abdul-Basith 1 Reputation point
2022-04-26T16:20:35.583+00:00

Hi,
I've been setting up vcheck reports and noticed that all the variables are stored in the global variables.ps1 file, what I'd like to understand is how are these variables referenced in the plugin scripts and other vcheck scripts?
This isn't a vcheck only question though, I'm trying to understand how powershell allows calling variables from an external file, this is something that I dont know how to do and would help me with scripting in general.

Thanks in advance!

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2022-04-26T17:21:23.52+00:00

    Hi @Mohammed Abdul-Basith ,

    maybe this helps to get an idea how global variables are working: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes?view=powershell-7.2

    And an example how to reference to a global variable: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes?view=powershell-7.2#example-5-using-a-local-variable-in-a-remote-command

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

  2. Rich Matheisen 47,901 Reputation points
    2022-04-26T18:22:48.167+00:00

    Those are probably "dot-sourced" in the other scripts.

    Something like this:

    . .\variables.ps1        # contains a variable declaration "$x = 1"
    
    $y = $x + 2
    write-host $y           # value of $y should be 3
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.