How to run PowerShell in CMD

后端 未结 3 657
抹茶落季
抹茶落季 2020-12-05 04:16

I\'m trying to run a PowerShell script inside cmd command line. Someone gave me an example and it worked:

powershell.exe -noexit \"& \'c:\\Data\\Schedule         


        
相关标签:
3条回答
  • 2020-12-05 04:24

    I'd like to add the following to Shay Levy's correct answer: You can make your life easier if you create a little batch script run.cmd to launch your powershell script:

    @echo off & setlocal
    set batchPath=%~dp0
    powershell.exe -noexit -file "%batchPath%SQLExecutor.ps1" "MY-PC"
    

    Put it in the same path as SQLExecutor.ps1 and from now on you can run it by simply double-clicking on run.cmd.


    Note:

    • If you require command line arguments inside the run.cmd batch, simply pass them as %1 ... %9 (or use %* to pass all parameters) to the powershell script, i.e.
      powershell.exe -noexit -file "%batchPath%SQLExecutor.ps1" %*

    • The variable batchPath contains the executing path of the batch file itself (this is what the expression %~dp0 is used for). So you just put the powershell script in the same path as the calling batch file.

    0 讨论(0)
  • 2020-12-05 04:29

    You need to separate the arguments from the file path:

    powershell.exe -noexit "& 'D:\Work\SQLExecutor.ps1 ' -gettedServerName 'MY-PC'"
    

    Another option that may ease the syntax using the File parameter and positional parameters:

    powershell.exe -noexit -file "D:\Work\SQLExecutor.ps1" "MY-PC"
    
    0 讨论(0)
  • 2020-12-05 04:34

    Try just:

    powershell.exe -noexit D:\Work\SQLExecutor.ps1 -gettedServerName "MY-PC"
    
    0 讨论(0)
提交回复
热议问题