Here is my code:
# hi.ps1 function fun { Write-Host $args } Write-Host '---------------' Write-Host $args Write-Host '---------------' fun Write-Host '---------------'
This is the output:
PS C:\scratch> .\hi there jim --------------- there jim --------------- ---------------
My question is, why isn’t $args visible within fun?
It seems that $args has ‘script’ scope. There are three scopes: local, global, and script.
If the function is changed like this:
Then it will work. The ‘script:’ is a scope resolution operator (‘local:’ is the default). I found that result somewhat experimentally, and I can’t say I know what the point of script scope is exactly, but it appears that variables in script scope belong to the script itself in some way.
Edit: I’m accepting my own answer as the best one since it is what solved my problem, but also see the other answers and their comments for some useful information.