You can pass the parameters in the PowerShell function and to catch those parameters, you need to use the arguments. Generally, when you use variables outside the function, you really don't need to pass the argument because the variable is itself a Public and can be accessible inside the function.
The $_ is a variable or also referred to as an operator in PowerShell that is used to retrieve only specific values from the field. It is piped with various cmdlets and used in the “Where” , “Where-Object“, and “ForEach-Object” clauses of the PowerShell.
The ${} notation actually has two uses; the second one is a hidden gem of PowerShell: That is, you can use this bracket notation to do file I/O operations if you provide a drive-qualified path, as defined in the MSDN page Provider Paths.
Description. The Get-Variable cmdlet gets the PowerShell variables in the current console. You can retrieve just the values of the variables by specifying the ValueOnly parameter, and you can filter the variables returned by name.
The variables declared in Variables.ps1
are at "Script Scope". That is you can not see them outside of the scope of the script that declares them. One way to bring the variables in Variables.ps1
to the scope of main.ps1
is to "dot source" Variables.ps1
. This, in effect, runs Variables.ps1
at the scope of main.ps1
. To do this, just stick a period and space before your invocation of the script:
. .\Variables.ps1
$var1
$var2
# var.ps1
$Global:var1 = "1"
$Global:var2 = "2"
This works. Whether it's better or worse than "dot sourcing" probably depends on your specific requirements.
PS > .\var.ps1
PS > $var1
1
PS > $var2
2
PS >
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With