I'm newer to PowerShell, transitioning from batch, so forgive me if I make a stupid mistake. I really like using Powershell ISE. But with the current script I'm trying to run, I use the cmd /c pause | Out-Nul command and it breaks ISE by not allowing me to continue any further.
I was trying for a long time to find some way to pause but not show output from the command and I found the cmd /c pause command. I can run it fine in a normal PowerShell prompt but ISE just isn't having it.
Anyone have any ideas why this happens or a better alternative to cmd /c pause Out-Nul? Thanks and any advice is more than welcome!
Powershell ISE doesn't redirect STDIN (which pause needs).
Shelling out to cmd for the pause command is unneeded.
See this post for more info on the ISE not being able to take stdin. You won't find your specific issue but the problem is the EXACT same problem.
On that page, you will also find a better/native way to do "pause" from powershell...
(aka [System.Console]::ReadKey($false) )
Related
I know it is possible to use a batch file, but due to a suggestion I switched a lot of my script over to PowerShell. The problem I ran into is that PowerShell is still missing some commands from CMD and has lower permissions when run as an Admin. Below is the current line I have been using.
powershell -Command "Start-Process 'cmd' -Verb RunAs -ArgumentList 'del /s "C:\Users\*.mp3"'"
I ran it in both 5.1 and 7.
It works with a simple command like opening the calculator or sending a ping, but I can't get the del command to work. The goal is to open a CMD window which will then delete all mp3 files. I know there are probably better ways to do this, but I more so want to know if it's possible than efficient. Thank you for your time!
This works somewhat
Start-Process cmd "/c del /S C:\Users\*.mp3"
Allow me to first address this: I am not asking "powershell close when script finish running". I know a lot of people are asking about this, but that have nothing to do with this question.
In ubuntu, terminal will prompt user to confirm the close of a terminal window if something is still running, so that user will not terminate their script by mistake. I've long been benefit from this mechanism.
Does powershell have similar setting? How to enable it?
Edit: I'm using powershell as a terminal, I may running a python script, powershell script or something else other than a script.
And although powershell does not accept the alt+F4 shortcut, powershell ISE does, still with no user prompt. And the whole powershell ISE can even exit entirely by simply typing 'exit' in one powershell tab within the ISE. Is there a way to prevent this?
I am using
Start-Process "<PathToFile>.bat"
For .bat files from a lengthy script in Powershell (v3). However, the commandline window pops up for a moment and is immediately closed and the process that normally runs on the commandline, runs in the background with no indication wether it's finished or if any errors occured.
Is there a way to force the command window to stay open until the user exits the window (after the .bat ran)? I suppose even if there is a way that the command window stays open, the PS script will continue to run in the background?
As said by Alex K CMD /K opens a CMD window and then keeps it open.
If you use CMD /C it will call the file, run the commands/process and then exit.
Sadly, it never worked for me with cmd /c. Since I also needed parameters to be handed over to the programm called from the commandline, I opted to write a temporary proxy bat that held the call to the program and the parameters.
Pseudocode here:
"ProgramName /Parameters" | Out-File DirOfTempBatFile -Encoding ascii
$output = Start-Process DirOfTempBatFile -wait
This calls the program (sqlplus with parameters) with keeping the command window open. Additionally, you can access the output of the sqlplus debug messages via $output.
I am writing a script to use multiple plink (PuTTY) sessions as a Windows version of clusterssh. I am stuck however because I want to open multiple Powershell windows from powershell. When I type the command for powershell, it opens a new session. This is similar to typing bash in bash. I want multiple physical windows opening.
I tried -windowstyle as well as the other args to no avail. I was wondering if there is a way you know of. I really appreciate your help. I looked and didn't find anything already here. Thanks for your time.
This will open a new window.
Either:
start-process powershell
Or:
start powershell
if you are trying to open a new window and launch a new script:
start powershell {.\scriptInNewPSWindow.ps1}
This will do it:
Invoke-Item C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
This works for me:
$argList = "-file `"$Location\script.ps1`""
Start-Process powershell -argumentlist $argList
(The backticks are necessary. This can be copied outright.) Variables can be used in the "-file" parameter (such as one set at the beginning of the script to reflect the location of the file) and spaces can appear in the variable due to the backticks.
Edited to use a two-line solution (the "$argList" variable) because PowerShell can mangle things otherwise.
To start Powershell 6 from a PS console start pwsh might do the trick.
It starts in the same folder.
(I haven't delved into it but I guess PS6's pwsh.exe has to be in the path for it to work.)
Every time I run an application (.exe) in PowerShell, instead of executing it as it does with scripts, the program gets run in Command Prompt.
Invoke-Item MyProgram.exe
I thought PowerShell was supposed to replace the Command Prompt but instead PowerShell opens Command Prompt which then runs the program. Is there some way I can get PowerShell to completely replace the Command Prompt?
I thought PowerShell was supposed to replace the Command Prompt
It is, so why don't you just do MyProgram.exe
Your question is pretty vague, and in short, it will depend on the program because Invoke-Item uses the Windows default file association stuff by default.
I'm betting that using & MyProgram.exe instead of Invoke-Item MyProgram.exe will work though.
Did you try Start-Process Program.exe -NoNewWindow
I got the answer from another question:
Start-Process .\MyProgram.exe -NoNewWindow -Wait