2016-03-20 18 views
5

Vorrei avere un file .bat cheCome avviare PowerShell in una nuova finestra da .Bat ed eseguire un .ps1 senza uscire dalla console?

  • Aprire una console PowerShell in una nuova finestra
  • E per eseguire uno script ps1
  • E non uscire e chiudere il Powershell

il mio file bat ha questa linea:

start powershell.exe -Command "&'D:\MyToolkit\ToolKit.ps1'" 

Tuttavia chiude il Potenza rshell dopo aver eseguito lo script.

Qualche consiglio? Grazie

+2

'inizio PowerShell -noexit -file "D: \ MyToolkit \ ToolKit.ps1"' – Avshalom

+1

Grazie! Lavorato! Non ha funzionato quando l'ho messo alla fine, ma quando l'ho messo prima -Commandit ha funzionato – pencilCake

risposta

8
start powershell -noexit -file "D:\MyToolkit\ToolKit.ps1" 

Inoltre,

cambiare il -Command--File come questo è quello che vi serve

2

Non solo per il manifesto originale di questa domanda, ma per gli altri che potrebbero atterrare qui in cerca di risposte , il sistema di aiuto è molto utile e sembra essere spesso trascurato.

Utilizzo del comando /? o in PowerShell i comandi get-help e get-help -full sono tutti utili.

Probabilmente avresti potuto rispondere alla tua domanda leggendo la guida per il comando che volevi eseguire, powershell in questo caso.

PowerShell[.exe] [-PSConsoleFile <file> | -Version <version>] 
    [-NoLogo] [-NoExit] [-Sta] [-Mta] [-NoProfile] [-NonInteractive] 
    [-InputFormat {Text | XML}] [-OutputFormat {Text | XML}] 
    [-WindowStyle <style>] [-EncodedCommand <Base64EncodedCommand>] 
    [-File <filePath> <args>] [-ExecutionPolicy <ExecutionPolicy>] 
    [-Command { - | <script-block> [-args <arg-array>] 
        | <string> [<CommandParameters>] } ] 

PowerShell[.exe] -Help | -? | /? 

-NoExit 
    Does not exit after running startup commands. 

... 

-File 
    Runs the specified script in the local scope ("dot-sourced"), so that the 
    functions and variables that the script creates are available in the 
    current session. Enter the script file path and any parameters. 
    File must be the last parameter in the command, because all characters 
    typed after the File parameter name are interpreted 
    as the script file path followed by the script parameters. 

-NoExit - non esce dopo l'esecuzione di comandi di avvio.

-File - ... file deve essere l'ultimo parametro nel comando ...