/// <summary>If the value of File is <c>-</c>, the command text is read from standard input. Running <c>powershell -File -</c> without redirected standard input starts a regular session. This is the same as not specifying the <c>File</c> parameter at all.<para/>If the value of File is a file path, the script runs in the local scope (<c>dot-sourced</c>), so that the functions and variables that the script creates are available in the current session. Enter the script file path and any parameters. <c>File</c> must be the last parameter in the command. All values typed after the <c>File</c> parameter are interpreted as the script file path and parameters passed to that script. Parameters passed to the script are passed as literal strings, after interpretation by the current shell. For example, if you are in cmd.exe and want to pass an environment variable value, you would use the cmd.exe syntax: <c>powershell.exe -File .\test.ps1 -TestParam %windir%</c>.<para/>In contrast, running <c>powershell.exe -File .\test.ps1 -TestParam $env:windir</c> in cmd.exe results in the script receiving the literal string <c>$env:windir</c> because it has no special meaning to the current cmd.exe shell. The <c>$env:windir</c> style of environment variable reference can be used inside a Command parameter, since there it will be interpreted as PowerShell code.<para/>Similarly, if you want to execute the same command from a Batch script, you would use <c>%~dp0</c> instead of <c>.\</c> or <c>$PSScriptRoot</c> to represent the current execution directory: <c>powershell.exe -File %~dp0test.ps1 -TestParam %windir%</c>. If you instead used <c>.\test.ps1</c>, PowerShell would throw an error because it cannot find the literal path <c>.\test.ps1</c>.<para/>When the value of <c>File</c> is a file path, <c>File</c> must be the last parameter in the command because any characters typed after the <c>File</c> parameter name are interpreted as the script file path followed by the script parameters.<para/>You can include the script parameters and values in the value of the <c>File</c> parameter. For example: <c>-File .\Get-Script.ps1 -Domain Central</c>.<para/>Typically, the switch parameters of a script are either included or omitted. For example, the following command uses the <c>All</c> parameter of the <c>Get-Script.ps1</c> script file: <c>-File .\Get-Script.ps1 -All</c>.<para/>In rare cases, you might need to provide a Boolean value for a parameter. It is not possible to pass an explicit boolean value for a switch parameter when running a script in this way. This limitation was removed in PowerShell 6 (<c>pwsh.exe</c>).</summary>
0 commit comments