Is it possible to pass a variable or function powershell -File $file
instead of constant string powershell -File 'C:\Script\test.ps1'
as the file parameter?
The following command works fine
C:\Script>PowerShell.exe -ExecutionPolicy Bypass -windowstyle hidden "$file = Get-ChildItem -Path C:\Script -Include test.ps1 -Recurse | Select-Object -ExpandProperty FullName"; Invoke-Item $file
but, if I change "Invoke-Item" with "-File" it doesn't work and I get an error Command:
C:\Script>PowerShell.exe -ExecutionPolicy Bypass -windowstyle hidden "$file = Get-ChildItem -Path C:\Script -Include test.ps1 -Recurse | Select-Object -ExpandProperty FullName"; -File $file
Error:
-File : Termine '-File' non riconosciuto come nome di cmdlet, funzione, programma eseguibile o file script. Controllare l'ortografia del nome o verificare che il percorso sia incluso e corretto, quindi
riprovare.
In riga:1 car:115
+ ... st.ps1 -Recurse | Select-Object -ExpandProperty FullName; -File $file
+ ~~~~~
+ CategoryInfo : ObjectNotFound: (-File:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Is it possible to pass a variable or function powershell -File $file
instead of constant string powershell -File 'C:\Script\test.ps1'
as the file parameter?
The following command works fine
C:\Script>PowerShell.exe -ExecutionPolicy Bypass -windowstyle hidden "$file = Get-ChildItem -Path C:\Script -Include test.ps1 -Recurse | Select-Object -ExpandProperty FullName"; Invoke-Item $file
but, if I change "Invoke-Item" with "-File" it doesn't work and I get an error Command:
C:\Script>PowerShell.exe -ExecutionPolicy Bypass -windowstyle hidden "$file = Get-ChildItem -Path C:\Script -Include test.ps1 -Recurse | Select-Object -ExpandProperty FullName"; -File $file
Error:
-File : Termine '-File' non riconosciuto come nome di cmdlet, funzione, programma eseguibile o file script. Controllare l'ortografia del nome o verificare che il percorso sia incluso e corretto, quindi
riprovare.
In riga:1 car:115
+ ... st.ps1 -Recurse | Select-Object -ExpandProperty FullName; -File $file
+ ~~~~~
+ CategoryInfo : ObjectNotFound: (-File:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Share
Improve this question
asked 4 hours ago
FCF75FCF75
11 silver badge2 bronze badges
2 Answers
Reset to default 0Use the invocation operator (&
)!
Change:
Invoke-Item $file
to:
& $file
To ensure you only ever attempt to execute one script, use Select-Object
's -First
parameter:
$file = Get-ChildItem -Path C:\Script -Include test.ps1 -Recurse | Select-Object -ExpandProperty FullName -First 1; & $file
If you want to invoke multiple scripts, wrap each call in a ForEach-Object
block:
Get-ChildItem -Path C:\Script -Include test.ps1 -Recurse | Select-Object -ExpandProperty FullName |ForEach-Object { & $_ }
The short answer is no—you cannot use a variable (or function) in place of a literal string for the -File parameter when launching PowerShell.exe. The -File switch is handled by the PowerShell executable itself before any of your inline script code (and thus any variable assignments) is executed.
Because -File is processed before the session starts, it cannot resolve any variables defined in the command string. If you need dynamic behavior, compute the file path first (outside of the PowerShell call) or use -Command where variable expansion is available.