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?
Running a PowerShell script from the Command Prompt If you would like to run a PowerShell script in CMD, you'll need to execute it by calling the PowerShell process with the -File parameter, as shown below: PowerShell -File C:\TEMP\MyNotepadScript. ps1. PowerShell -File C:\TEMP\MyNotepadScript.
You can run scripts with parameters in any context by simply specifying them while running the PowerShell executable like powershell.exe -Parameter 'Foo' -Parameter2 'Bar' . Once you open cmd.exe, you can execute a PowerShell script like below.
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 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