Kind Regards.
-
3Please, [DO NOT post images of code, data, error messages, etc.](https://meta.stackoverflow.com/a/285557), see [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – iRon Jun 16 '23 at 15:16
-
1Why not simply:`PowerShell -File $env:userprofile\git\...`? – iRon Jun 16 '23 at 15:25
-
What happens if you use `& ${Env:USERPROFILE}\Git\Scr...` or `& "$Env:USERPROFILE\Git\...` or `& "${Env:USERPROFILE}\Git\...`? – lit Jun 16 '23 at 16:00
1 Answers
You're calling powershell.exe
from PowerShell:
First, the question is why you're calling another instance of PowerShell in order to execute a script file (
*.ps1
). You could just invoke it directly, which is not only simpler, but faster, due to running in-process ( a(
in the user name reflected in$env:USERPROFILE
is then not a problem):& $env:USERPROFILE\Git\Scripte\PowerShell\Wallpaper_Slideshow.ps1
If you do need another instance - such as if you're calling from PowerShell (Core) 7+, but your script only works in Windows PowerShell - use a script block (
{ ... }
), which not only avoids your problem, but also preserves rich data-type support for both input and output (within the constraints of the XML-based cross-process serialization that happens behind the scenes).powershell { & $env:USERPROFILE\Git\Scripte\PowerShell\Wallpaper_Slideshow.ps1 }
- Note that this technique works from PowerShell only. For outside calls, a string-based invocation, such as the one you tried, is necessary.
As for what you tried:
powershell "& $env:USERPROFILE\Git\Scripte\PowerShell\Wallpaper_Slideshow.ps1"
If you specify neither
-File
nor-Command
withpowershell.exe
,-Command
is implied (note that forpwsh.exe
, the PowerShell (Core) 7+ CLI, it is now-File
).What follows
-Command
, after removal of (unescaped)"
during command-line parsing, is then interpreted as PowerShell code.Because you're calling from PowerShell and are using an expandable (double-quoted) string (
"..."
), the reference to environment variable$env:USERPROFILE
is expanded before the child process sees the resulting command-line string, which is the source of your problem:PowerShell ends up executing something like
& C:\User\user(a\...
, which - due to the unescaped(
- causes a syntax error.Therefore, there are two solutions (though the script-block technique shown above is still preferable:
Either: Use
'...'
rather than"..."
around the (implied)-Command
argument.powershell '& $env:USERPROFILE\Git\Scripte\PowerShell\Wallpaper_Slideshow.ps1'
Or: Escape the
$
in$env:USERPROFILE
as`$
to prevent premature expansion (this is helpful if you need selective up-front expansion):# Note the ` (backtick) before $ powershell "& `$env:USERPROFILE\Git\Scripte\PowerShell\Wallpaper_Slideshow.ps1"
An alternative, as iRon points out, is to use the -File
parameter:
powershell -File $env:USERPROFILE\Git\Scripte\PowerShell\Wallpaper_Slideshow.ps1
What is passed to
-File
- both the script file path itself and any subsequent pass-through arguments, are not subject to another round of interpretation as PowerShell code, so the(
in the environment-variable value doesn't cause a problem.For detailed guidance on when to use
-File
vs.-Command
, see this answer.

- 382,024
- 64
- 607
- 775