So I've tried a bunch of different ways to run a PowerShell script from the command line and every single one returns an error.
Here is this path:
C:\Users\test\Documents\test\line space\PS Script\test.ps1
I've tried these:
powershell -File '"C:\Users\test\Documents\test\line space\PS Script\test.ps1"' powershell "& ""C:\Users\test\Documents\test\line space\PS Script\test.ps1""" Powershell "& 'C:\Users\test\Documents\test\line space\PS Script\test.ps1'" Powershell -File 'C:\Users\test\Documents\test\line space\PS Script\test.ps1'"
I get all these errors:
& : The term 'C:\Users\test\Documents\test\line space\PS Script' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
Processing -File ''C:\Users\test\Documents\test\line space\PS Script'' failed: The given path's format is not support ed. Specify a valid path for the -File parameter.
How can I fix this?
PowerShell: Use the Grave Accent Character ( ` ) PowerShell uses the grave accent ( ` ) character as its escape character. Just add it before each space in the file name. (You'll find this character above the Tab key and below the Esc key on your keyboard.)
Running a script with PowerShell To open the PowerShell console, click on the Start button (or search button), type powershell, and click Run as Administrator. To run a script in the PowerShell console, you can either: Use the full path to script, like: C:\TEMP\MyNotepadScript.
Spaces around parameters and operators You should use a single space around parameter names and operators, including comparison operators and math and assignment operators, even when the spaces are not necessary for PowerShell to correctly parse the code. # Same principle should be applied when using a variable.
-File
parameterIf you want to run powershell.exe -File
from the command line, you always have to set paths with spaces in double quotes ("
). Single quotes ('
) are only recognized by PowerShell. But as powershell.exe is invoked (and hence the file parameter processed) by the command line, you have to use "
.
powershell.exe -File "C:\Users\test\Documents\Test Space\test.ps1" -ExecutionPolicy Bypass
-Command
parameterIf you use the -Command
parameter, instead of -File
, the -Command
content is processed by PowerShell. Hence you can - and in this case have to - use '
inside "
.
powershell.exe -Command "& 'C:\Users\test\Documents\Test Space\test.ps1'" -ExecutionPolicy Bypass
The double quotes are processed by the command line, and & 'C:\Users\test\Documents\Test Space\test.ps1'
is a command that is actually processed by PowerShell.
Solution 1 is obviously simpler.
Note that -Command
is also the default parameter that is used, if you do not specify any.
powershell.exe "& 'C:\Users\test\Documents\Test Space\test.ps1'" -ExecutionPolicy Bypass
This would work, too.
-EncodedCommand
parameterYou can encode your command as Base64. This solves many "quoting" issues and is sometimes (but not in your case though) the only possible way.
First you have to create the encoded command
$Command = "& 'C:\Users\test\Documents\Test Space\test.ps1'" [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($Command))
And then you can use the the -EncodedCommand
parameter like this
powershell.exe -EncodedCommand JgAgACcAQwA6AFwAVQBzAGUAcgBzAFwAdABlAHMAdABcAEQAbwBjAHUAbQBlAG4AdABzAFwAVABlAHMAdAAgAFMAcABhAGMAZQBcAHQAZQBzAHQALgBwAHMAMQAnAA== -ExecutionPolicy Bypass
Try this:
& "C:\Users\test\Documents\test\line space\PS Script\test"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With