How to run a PowerShell script?

How do I run a PowerShell script?

  • I have a script named myscript.ps1
  • I have all the necessary frameworks installed
  • I set that execution policy thing
  • I have followed the instructions on this MSDN help page and am trying to run it like so: powershell.exe 'C:my_pathyada_yadarun_import_script.ps1' (with or withot --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?


  • Launch Windows PowerShell, and wait a moment for the PS command prompt to appear
  • Navigate to the directory where the script lives

    PS> cd C:my_pathyada_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_pathyada_yadarun_import_script.ps1""" (enter)
    

    according to this blog post here

    Or you could even run your Powershell script from your C# app :-)

    Asynchronously execute PowerShell scripts from your C# application


    如果您使用PowerShell 2.0,请使用PowerShell.exe的-File参数从另一个环境(如cmd.exe)调用脚本,例如:

    Powershell.exe -File C:my_pathyada_yadarun_import_script.ps1
    

    如果要在不修改默认脚本执行策略的情况下运行脚本,则可以在启动Windows PowerShell时使用旁路开关。

    powershell [-noexit] -executionpolicy bypass -File <Filename>
    
    链接地址: http://www.djcxy.com/p/4940.html

    上一篇: 我如何使用.NET 4运行时运行PowerShell?

    下一篇: 如何运行PowerShell脚本?