How do I run a PowerShell script?
powershell.exe 'C:\my_path\yada_yada\run_import_script.ps1'
(with or without --noexit
)which returns exactly nothing, except that the file name is output.
No error, no message, nothing. Oh, when I add -noexit
, the same thing happens, but I remain within PowerShell and have to exit manually.
The.ps1 file is supposed to run a program and return the error level dependent on that program's output. But I'm quite sure I'm not even getting there yet.
What am I doing wrong?
PS
command prompt to appearNavigate to the directory where the script lives
PS> cd C:\\my_path\\yada_yada\\ (enter)
Execute the script:
PS> .\\run_import_script.ps1 (enter)
What am I missing??
Or: you can run the PowerShell script from cmd.exe
like this:
powershell -noexit "& ""C:\my_path\yada_yada\run_import_script.ps1""" (enter)
according to this blog post here
Or you could even run your PowerShell script from your C# application :-)
Asynchronously execute PowerShell scripts from your C# application
If you are on PowerShell 2.0, use PowerShell.exe's -File
parameter to invoke a script from another environment, like cmd.exe. For example:
Powershell.exe -File C:\my_path\yada_yada\run_import_script.ps1
如果您想在不修改默认脚本执行策略的情况下运行脚本,您可以在启动Windows PowerShell时使用绕过开关。
powershell [-noexit] -executionpolicy bypass -File <Filename>
Type:
powershell -executionpolicy bypass -File .\\Test.ps1
NOTE: Here Test.ps1
is the PowerShell script.
I've had the same problem, and I tried and tried... Finally I used:
powershell.exe -noexit "& 'c:\Data\ScheduledScripts\ShutdownVM.ps1'"
And put this line in a batch-file, and this works.
If you only have PowerShell 1.0 , this seems to do the trick well enough:
powershell -command - < c:\mypath\myscript.ps1
It pipes the script file to the PowerShell command line.
Pretty easy. Right click the .ps1 file in Windows and in the shell menu click on Run with PowerShell .
Using cmd (BAT) file:
@echo off
color 1F
echo.
C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File "PrepareEnvironment.ps1"
:EOF
echo Waiting seconds
timeout /t 10 /nobreak > NUL
If you need run as administrator :
If your script is named with the .ps1
extension and you're in a PowerShell window, you just run ./myscript.ps1
(assuming the file is in your working directory).
This is true for me anyway on Windows 10 with PowerShell version 5.1 anyway, and I don't think I've done anything to make it possible.
An easy way is to use PowerShell ISE , open script, run and invoke your script, function...
In case you want to run a PowerShell script with Windows Task Scheduler, please follow the steps below:
Create a task
Set Program/Script
to Powershell.exe
Set Arguments
to -File "C:\\xxx.ps1"
It's from another answer, How do I execute a PowerShell script automatically using Windows task scheduler? .
Give the path of the script, that is, path setting by cmd:
$> . c:\\program file\\prog.ps1
Run the entry point function of PowerShell:
For example, $> add or entry_func or main
I have a very simple answer which works:
set-executionpolicy unrestricted
I found this solution following the link that was given as part of error message: About Execution Policies
Edit: Make sure to run set-ExecutionPolicy default
once you're done, or you will be exposed to seurity risks (thanks Anonymous user).
您可以像这样从 cmd 运行:
type "script_path" | powershell.exe -c -
Use the -File
parameter in front of the filename. The quotes make PowerShell think it is a string of commands.
With the appropriate execution policy, you should just be able to call the file directly and Windows will associate it with PowerShell
C:\my_path\yada_yada\run_import_script.ps1
That does not do so well with arguments. The real answer to your question is that you are missing the &
to say "execute this"
powershell.exe '& C:\my_path\yada_yada\run_import_script.ps1'
This should execute your simple powershell script.
I've just found the method what Microsoft do when we right click on a ps1
script and click on "Run with PowerShell":
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-Command" "if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & 'C:\Users\USERNAME\Desktop\MYSCRIPT.ps1'"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.