How I can I start a powershell command line from a batch file and enforce it to stay open? - powershell

How I can I start a powershell session from a batch file so that it starts the Powershell command line (and imports some modules and then) stays open instead being closed once the commands are run in the bat. So that I can continue to work on the PS Shell command line with the modules imported.
Thanks

You could try this.
-noexit will stop powershell for terminating.
start powershell.exe -noexit

Related

Batch File - Commands not executing after Powershell command

I have written simple batch script to replace some text in a file, when executed via command line but when those commands are copied to .bat file execution stops after powershell command. Any idea how to execute powershell commands in batch file?
arco444's answer worked for me in comments:
You just call powershell.exe with no parameters so it starts an interactive session. If you were to type exit your batch script would continue. Use the -command switch to execute whatever it is you want to. And please edit your question and include the code there. Also You need to put the powershell command in quotes. i.e. powershell -command "get-content file.cs"

how to run a powershell script and a batch script in a batch script?

i have controller batch file named oneclick.bat, code as below:
rem batch controller
rem wait for email input for ssh key generation...
rem call copyEnv.bat
call generateSshKey.bat %1
call gitClone.bat
in generateSshKey.bat, i start a powershell script like this:
rem make sure powershell is able to run
powershell.exe set-executionpolicy remotesigned
rem start powershell and add ssh key to the server by ui aotumation
powershell.exe -noe -file SetSSHKeytoServer.ps1
and then gitClone.bat did not run in the command window
how can i get the gitClone.bat run?
When you want to return to a calling script you shouldn't run PowerShell with an option that prevents it from returning. Remove -noe from the last line of generateSshKey.bat.
-NoExit
Does not exit after running startup commands.
As Ansgar Wiechers pointed out, the -noe (short for -NoExit) is keeps the PowerShell session open, so generateSshKey.bat doesn't exit, and oneclick.bat doesn't get past the line that called it.
If you specifically want the powershell session that runs SetSSHKeytoServer.ps1 to remain active (since you used the -noe switch the second time but not the first time, I'm inferring that this was deliberate), you could change
call generateSshKey.bat %1
to
start generateSshKey.bat %1
Note that this means that oneclick.bat won't wait for generateSshKey.bat to finish before calling gitClone.bat, so if you need to run them in sequence, this won't work. It will work as long as it's okay for gitClone.bat to start while generateSshKey.bat is still running.

running a bat file and powershell with elevated priviliges

so i have a bat file that goes:
powershell -noexit "My\file\location\myscript.ps1"
the bat file is in the start up. the powershell script changes the bcd. when executed in this manner it says i do not have privileges, access denied, and nothing in the bcd is changed.
sorry for being a newb, but is there a way to pass an elevated privileges value from the bat to the powershell script?
I don't know if this is possible to do from outside Power Shell but it can be done from inside. It's hacky but you could run a power shell script from your bat file that then launches the admin Power Shell session using "PS> Start-Process powershell -Verb runAs".
PowerShell: Running a command as Administrator

Unable to load .ps1 powershell script from Powershell command (console)

I am stuck in this weird problem where I am trying to execute a powershell script from the powershell command prompt. But neither do I get any errors nor the script is loaded.
I have script in C:\temp\myFunction.ps1 (which has a method getMyName() )
I open the powershell command and navigate to this directory and execute
./myFunction.ps1
then there are no errors and return back to the next line in the prompt. But when I try to call the function getMyName - I get error getMyName is not recognised.
I have set the Execution-Policy to Unrestricted, I am running the powershell as Administrator
Try dot sourcing your script:
. .\myFunction.ps1
It's basic problem of Powershell script. Set the Path where you physically saved your file and then execute the Powershell script. One more thing
1. start your command window run as admin.
2. set the Powershell script policy for execution.

Running PowerShell Scripts in Komodo Edit

I have a hard time trying to run PowerShell Scripts from the "Run Command" within Komodo Edit on Windows 7.
The command that I am using is:
powershell -File "%F"
When I run it, it does not return anything to the console, it just keeps running till I terminate it.
I have tested it, with the following simple script:
Write-Host "Hello World"
This is a known issue where powershell.exe waits for a STDIN prompt to return in certain cases, causing it to hang when no input is provided.
Solutions
Use -InputFormat None to indicate STDIN will not be used:
powershell.exe -InputFormat None -File "%F"
Forward null input from the outer command scope so that STDIN returns:
powershell.exe -File "%F" < NUL
Similar Questions
2010-11-09 Silently executing a PowerShell script from WiX Hangs PowerShell
2010-11-21 Running PowerShell from MSdeploy runcommand does not exit
2011-07-26 Exiting VMware vSphere PowerCLI command prompt?