I have two PowerShell scripts. First script takes two parameters from the command-line and passes them to the second script.
Script1.ps1:
Write-Output ($args)
Write-Output ($args.Length)
. ./Script2.ps1 $args
Script2.ps1:
Write-Output ($args)
Write-Output ($args.Length)
Calling this like
Script1.ps1 hi script1
Output of Script1.ps1:
hi script1 2
Output of Script2.ps1:
System.Object[] 1
Questions:
- Why can't I use
$argsdirectly onScript2.ps1(comes as null)? - Why does
$argspassed thoughScript1.ps1come as a single string inScript2.ps1?
This works fine in PowerShell 2.0.
$argsdirectly onScript2.ps1"?$argsinScript2.ps1without been passed fromScript1.ps1$argsis an automatic variable that PowerShell populates with the arguments to a script. And even if you could it would be a bad idea to pass information via global variables.