If browser is not already open. make the browser open.
If browser is already open. make the browser close.
I've found a script but this does not close the browser
!f::
firefox = C:\Program Files\Mozilla Firefox\Firefox.exe
IfWinExist, firefox ; Title of the prog
{
WinActivate
WinClose
}
else
{
Run, C:\Program Files\Mozilla Firefox\Firefox.exe "github.com"
Run, C:\Program Files\Mozilla Firefox\Firefox.exe "stackoverflow.com"
}
How can I close the program already exists?
Try with this:
!f::
firefox = C:\Program Files\Mozilla Firefox\Firefox.exe
IfWinExist, "ahk_exe firefox.exe" ; Executable name
{
WinActivate
WinClose
}
else
{
Run, C:\Program Files\Mozilla Firefox\Firefox.exe "github.com"
Run, C:\Program Files\Mozilla Firefox\Firefox.exe "stackoverflow.com"
}
Related
I am simply trying to code something that can either launch the Chrome browser to the "about" page; or open a tab with the "about" page, that will run in PowerShell.
I have tried the following, and it only results in chrome opening for a short second, and then closing...
`
Start-Process -FilePath "C:\Program Files\Google\Chrome\Application\chrome.exe" -ArgumentList '"chrome://about"'
`
This works with any HTTP/S: location, but I think it doesn't like the 'Chrome:' tag.
... Any ideas?
It's because that landing page/tab is not a legit URL.
So you can do this (though well, you know, SendKeys ...).
Add-Type -AssemblyName System.Windows.Forms
Start-Process -FilePath 'C:\Program Files\Google\Chrome\Application\chrome.exe'
Start-Sleep -Milliseconds 300
[System.Windows.Forms.SendKeys]::SendWait('chrome://about{Enter}')
I'm having a little problem about closing gently Chrome in order to automatically clean cache.
I have a website that is not refreshing correctly, but it does if I clean the cache.
For now I have a .bat file with the following code:
taskkill /F /IM chrome.exe /T > nul
del /q "C:\Users\Francisco\AppData\Local\Google\Chrome\User Data\Default\Cache\*"
FOR /D %%p IN ("C:\Users\Francisco\AppData\Local\Google\Chrome\User Data\Default\Cache\*.*") DO rmdir "%%p" /s /q
timeout 8
start chrome --restore-last-session --start-fullscreen
Now, I know that taskkill /F forces the process to close and that works but as soon as Chrome opens, it shows a message that Chrome wasn't closed correctly and asks me if I want to restore the last session.
If i remove the /f option, Chrome doesn't close:
ERROR: the process with PID 8580 (PID secondary process 8896)
Could not finish.
Reason: This process can be terminated only forcibly (with the / F option).
So... I need one of two options;
Is there a way to gently close Chrome like pressing Alt+F4 using CMD or a VBS?2. Is there a way to hide that Chrome restore last session message?
I'd prefer the first option since it's cleaner.
you can use powershell to close all windows with chrome as process name. It won't kill the task but gently close all chrome browser windows, like if you clicked on top-right cross. Here is the command line to put in batch :
powershell -command "Get-Process chrome | ForEach-Object { $_.CloseMainWindow() | Out-Null}"
Refer to the answer of #tuxy42 :
You can write with vbscript to open chrome browser like this : Open_Chrome_Browser.vbs
Set ws = CreateObject("Wscript.Shell")
siteA = "https://stackoverflow.com/questions/62516355/google-chrome-always-says-google-chrome-was-not-shut-down-properly"
ws.Run "chrome -url " & siteA
And if you want to close it safely : safely_close_chrome_browser.vbs
Set ws = CreateObject("Wscript.Shell")
psCommand = "Cmd /C powershell -command ""Get-Process chrome | ForEach-Object { $_.CloseMainWindow() | Out-Null}"""
ws.run psCommand,0,True
Just browse URL
chrome://quit/
I would like to open Visual Studio Code for a specific folder, the command works I try to run, but when the script opens VSCode, it doesn't close the PowerShell window. I would like to close the PowerShell window right after the Script opens the VSCode. But even an explicit exit call does not work:
ii F:\c#
iex "& code ."
exit
This opens VSCode, but the Shell Window stays open until I close VSCode.
This is the bug in the VS Code's backend. It unnecessary attaches itself to the console window preventing it to close even after shell application quit.
It actually not related to PowerShell. You will get the same behavior with .bat/.cmd file, which contains single code command. If you double-click it in explorer, then console windows will not close until you close VS Code, even though in task manager you can see, that CMD instance used to execute .bat/.cmd file no longer exists.
To workaround this bug you can create new hidden console window for VS Code to attach to, instead of console window of your PowerShell instance:
Start-Process -WindowStyle Hidden code .
To open your project by using powerShell,
Go to your project directory after press shift+right click
Select "Open PowerShell window here"
Just type code . and press enter.
Thanks,
Example:
function code {
$code_path = "${Env:LOCALAPPDATA}\Programs\Microsoft VS Code\bin\code.cmd" -replace ' ', '` '
"${code_path} $args" | Invoke-Expression
}
I created helper functions in my PowerShell $profile called idea
and vscode:
function idea {
Start-Process -FilePath "D:\Program Files\JetBrains\IntelliJ IDEA 2021.2.1\bin\idea64.exe" -ArgumentList "."
exit
}
function vsc {
Start-Process -FilePath "D:\Users\AppData\Local\Programs\Microsoft VS Code\Code.exe" -ArgumentList "."
exit
}
This works for me:
iex "code ."
$host.SetShouldExit(0)
Your problem is that calling exit will exit the script and not the PowerShell instance you called it from. You can define a function in a script file (or in your profile.ps1). Afterwards you source the script file via:
. .\StartCode.ps1
Content of StartCode.ps1:
function Start-CodeInViaStartProcess {
Start-Process -FilePath "code" -ArgumentList "."
exit
}
Since the content of StartCode.ps1 is now sourced to your PowerShell session exit will terminate the actuall PowerShell session. Therefore Start-CodeInViaStartProcess should terminate your PowerShell window.
How can one make a windows short-cut that opens Powershell into a specific directory?
Such as the target:
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe
-noexit -command {cd c:/path/to/open}
But that just spits out the command as text. How?
Use this command.
powershell.exe -noexit -command "cd c:\temp"
-NoExit: Do not exit after running startup commands.
You can also set the "Start in" shortcut field to your desired location.
Ok - you need to use the & parameter to specify it's a powershell comand & the syntax is slightly different:
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe
-noexit -command "& {cd c:\path\to\open}"
Define a Shortcut for Powershell, and Open the properties of that, and finally in "Start" type the folder target to be opened when Powershell Shortcut is triggered
If you want powershell to start as admin and run in a specific directory, even on a different drive, it is better to use the Set-Location command. Follow these steps
Create a ShortCutLink with the target being the powershellcommand exe.
Leave Start in: blank. (Normally this starts in current working directory when blank; but we do not care.)
Change Target to this with your targets for powershell and locations:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -command "Set-Location D:\_DCode\Main"
Click Advanced... and select Run as administrator.
Click OKs out.
Don't forget the handy trick to change the colors of the shortcut from the Colors tab. That way if you have two or more links which open powershell windows, seeing a different color can visually let you know which shell one is working in.
try:
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe
-noexit -command "cd c:/path/to/open"
If one wants a explorer right click options run this script:
New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT
if(-not (Test-Path -Path "HKCR:\Directory\shell\$KeyName"))
{
Try
{
New-Item -itemType String "HKCR:\Directory\shell\$KeyName" -value "Open PowerShell in this Folder" -ErrorAction Stop
New-Item -itemType String "HKCR:\Directory\shell\$KeyName\command" -value "$env:SystemRoot\system32\WindowsPowerShell\v1.0\powershell.exe -noexit -command Set-Location '%V'" -ErrorAction Stop
Write-Host "Successfully!"
}
Catch
{
Write-Error $_.Exception.Message
}
}
else
{
Write-Warning "The specified key name already exists. Type another name and try again."
}
This is what is shown now:
Note that you can download a detailed script from how to start PowerShell from Windows Explorer.
Copy this code into notepad and save with a reg extension.
Double click the resulting file.If you get a message about importing to the registry click on yes and then ok.
Navigate to any folder in explorer and bring up the context menu. This is typically done by clicking the right mouse button.
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\Background\shell\PShell]
"MUIVerb"="Open in Powershell Window"
[HKEY_CLASSES_ROOT\Directory\Background\shell\PShell\command]
#="c:\\windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe -NoExit -Command Set-Location -LiteralPath '%V'"
If you prefer to launch Windows Terminal with your prefered command line shell, you can use:
wt.exe -d "c:\temp"
I just wanted to add my Developer Powershell link ... for the records.
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -noe -c "&{Import-Module """C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\Tools\Microsoft.VisualStudio.DevShell.dll"""; Enter-VsDevShell d998f19b; cd c:\dev\}"
This will start the Developer Powershell (VS 2019) in c:\dev\.
If you are using Powershell 7 (pwsh), simply use the -WorkingDirectory flag like this:
pwsh -WorkingDirectory "C:\path\to\your\directory"
I use a .ps1 script file to open a PowerShell terminal at a specific path from a shortcut in the taskbar.
The script:
cd 'directory path'
powershell
Running "powershell.exe" from a PowerShell terminal will start a new PowerShell session, preventing the terminal window from closing.
I am trying to close firefox as soon as I open it, and then reopen it again in a batch file.
This is what i have below:
#ECHO off
"C:\Program Files\Mozilla Firefox\firefox.exe"
sleep 10
taskkill /F/IM "C:\Program Files\Mozilla Firefox\firefox.exe"
sleep 10
"C:\Program Files\Mozilla Firefox\firefox.exe"
However it doesnt work, it just opens firefox and never closes it.
The system I am trying to get this to work on is Windows 7 Home Premium 64bit. I don't know if that makes much of a difference.
Any help on this will be greatly appreciated. Thanks.
Taskkill is used to kill the process.
Try
taskkill /F/IM firefox.exe
Here is the documentation from MSDN
http://technet.microsoft.com/en-us/library/bb491009.aspx