Skip to main content
optimized code, same results.
Source Link

Update:

This is even simpler and is in line with what user MagTun said above. This also fixed path issues that occur when the '&' symbol is in the file path.

!c::_OpenCMDHere()

_OpenCMDHere()
{
    If WinActive("ahk_class CabinetWClass")
        _WinHWND := WinActive()
        For Win in ComObjCreate("Shell.Application").Windows
            If (Win.HWND = _WinHWND)
            {
                _pwd := SubStr(Win.LocationURL, 9)
                _pwd := RegExReplace(_pwd, "%20", " ")
                _pwd := RegExReplace(_pwd, "%26", "&") ; Fix issues with '&' in the path
                _pwd := RegExReplace(_pwd, "/", "\")
            }
    Run, "C:\Windows\System32\cmd.exe" /T:0A /D /K PROMPT $P$G$_$G, % _pwd ? _pwd : "C:\Windows\System32\", Max
}
Return

Update:

This is even simpler and is in line with what user MagTun said above. This also fixed path issues that occur when the '&' symbol is in the file path.

!c::_OpenCMDHere()

_OpenCMDHere()
{
    If WinActive("ahk_class CabinetWClass")
        _WinHWND := WinActive()
        For Win in ComObjCreate("Shell.Application").Windows
            If (Win.HWND = _WinHWND)
            {
                _pwd := SubStr(Win.LocationURL, 9)
                _pwd := RegExReplace(_pwd, "%20", " ")
                _pwd := RegExReplace(_pwd, "%26", "&") ; Fix issues with '&' in the path
                _pwd := RegExReplace(_pwd, "/", "\")
            }
    Run, "C:\Windows\System32\cmd.exe" /T:0A /D /K PROMPT $P$G$_$G, % _pwd ? _pwd : "C:\Windows\System32\", Max
}
Return

removed personal settings not required for general use
Source Link

This utilizes autohotkey.exe. Save this with .ahk extension.

If explorer.exe is the active window, open cmd.exe using the active PATH.

If explorer.exe is not the active window, open cmd.exe to C:\Windows\System32.

Run this by itself or call it with a master script using #Include PATH\TO\BELOW\SCRIPT.ahk

; ###################
; #  OPEN CMD HERE  #
; ###################

!c::OpenCMDHere_OpenCMDHere()

OpenCMDHere_OpenCMDHere()
{
    GroupAdd, GroupCMDHere_GroupCMDHere, ahk_exe cmd.exe
    GroupClose, GroupCMDHere_GroupCMDHere, A
    If WinActive("ahk_class CabinetWClass") {
        WinHWND_WinHWND := WinActive()
        For Win in ComObjCreate("Shell.Application").Windows
            If (Win.HWND = WinHWND_WinHWND)
                pwd_pwd := SubStr(Win.LocationURL, 9)
                pwd_pwd := RegExReplace(pwd_pwd, "%20", " ")
                pwd_pwd := RegExReplace(pwd_pwd, "/", "\")
            Run, "C:\Windows\System32\cmd.exe" /T:0A /D /K PROMPT $P$G$_$G & PUSHD "%pwd%""%_pwd%",, Max, CMDHerePIDWinPID
            WinWaitActiveWinWait, ahk_pid %_PidCMDHere%%WinPID%
            WinMoveWinActivate, ahk_pid %_PidCMDHere%,, -10, -4, %A_ScreenWidth%, %A_ScreenHeight%%WinPID%
    }
    Else {
        Run, "C:\Windows\System32\cmd.exe" /T:0A /D /K PROMPT $P$G$_$G & PUSHD "C:\Windows\System32\",, Max, CMDHerePIDWinPID
        WinWaitActiveWinWait, ahk_pid %_PidCMDHere%%WinPID%
        WinMoveWinActivate, ahk_pid %_PidCMDHere%,, -10, -4, %A_ScreenWidth%, %A_ScreenHeight%%WinPID%
    }
}
Return


This utilizes autohotkey.exe. Save this with .ahk extension.

If explorer.exe is the active window, open cmd.exe using the active PATH.

If explorer.exe is not the active window, open cmd.exe to C:\Windows\System32.

Run this by itself or call it with a master script using #Include PATH\TO\BELOW\SCRIPT.ahk

; ###################
; #  OPEN CMD HERE  #
; ###################

!c::OpenCMDHere()

OpenCMDHere()
{
    GroupAdd, GroupCMDHere, ahk_exe cmd.exe
    GroupClose, GroupCMDHere, A
    If WinActive("ahk_class CabinetWClass") {
        WinHWND := WinActive()
        For Win in ComObjCreate("Shell.Application").Windows
            If (Win.HWND = WinHWND)
                pwd := SubStr(Win.LocationURL, 9)
                pwd := RegExReplace(pwd, "%20", " ")
                pwd := RegExReplace(pwd, "/", "\")
            Run, "C:\Windows\System32\cmd.exe" /T:0A /D /K PROMPT $P$G$_$G & PUSHD "%pwd%",,, CMDHerePID
            WinWaitActive, ahk_pid %_PidCMDHere%
            WinMove, ahk_pid %_PidCMDHere%,, -10, -4, %A_ScreenWidth%, %A_ScreenHeight%
    }
    Else {
        Run, "C:\Windows\System32\cmd.exe" /T:0A /D /K PROMPT $P$G$_$G & PUSHD "C:\Windows\System32\",,, CMDHerePID
        WinWaitActive, ahk_pid %_PidCMDHere%
        WinMove, ahk_pid %_PidCMDHere%,, -10, -4, %A_ScreenWidth%, %A_ScreenHeight%
    }
}
Return


This utilizes autohotkey.exe. Save this with .ahk extension.

If explorer.exe is the active window, open cmd.exe using the active PATH.

If explorer.exe is not the active window, open cmd.exe to C:\Windows\System32.

Run this by itself or call it with a master script using #Include PATH\TO\BELOW\SCRIPT.ahk

; ###################
; #  OPEN CMD HERE  #
; ###################

!c::_OpenCMDHere()

_OpenCMDHere()
{
    GroupAdd, _GroupCMDHere, ahk_exe cmd.exe
    GroupClose, _GroupCMDHere, A
    If WinActive("ahk_class CabinetWClass") {
        _WinHWND := WinActive()
        For Win in ComObjCreate("Shell.Application").Windows
            If (Win.HWND = _WinHWND)
                _pwd := SubStr(Win.LocationURL, 9)
                _pwd := RegExReplace(_pwd, "%20", " ")
                _pwd := RegExReplace(_pwd, "/", "\")
            Run, "C:\Windows\System32\cmd.exe" /T:0A /D /K PROMPT $P$G$_$G & PUSHD "%_pwd%",, Max, WinPID
            WinWait, ahk_pid %WinPID%
            WinActivate, ahk_pid %WinPID%
    }
    Else {
        Run, "C:\Windows\System32\cmd.exe" /T:0A /D /K PROMPT $P$G$_$G & PUSHD "C:\Windows\System32\",, Max, WinPID
        WinWait, ahk_pid %WinPID%
        WinActivate, ahk_pid %WinPID%
    }
}
Return


Source Link

This utilizes autohotkey.exe. Save this with .ahk extension.

If explorer.exe is the active window, open cmd.exe using the active PATH.

If explorer.exe is not the active window, open cmd.exe to C:\Windows\System32.

Run this by itself or call it with a master script using #Include PATH\TO\BELOW\SCRIPT.ahk

; ###################
; #  OPEN CMD HERE  #
; ###################

!c::OpenCMDHere()

OpenCMDHere()
{
    GroupAdd, GroupCMDHere, ahk_exe cmd.exe
    GroupClose, GroupCMDHere, A
    If WinActive("ahk_class CabinetWClass") {
        WinHWND := WinActive()
        For Win in ComObjCreate("Shell.Application").Windows
            If (Win.HWND = WinHWND)
                pwd := SubStr(Win.LocationURL, 9)
                pwd := RegExReplace(pwd, "%20", " ")
                pwd := RegExReplace(pwd, "/", "\")
            Run, "C:\Windows\System32\cmd.exe" /T:0A /D /K PROMPT $P$G$_$G & PUSHD "%pwd%",,, CMDHerePID
            WinWaitActive, ahk_pid %_PidCMDHere%
            WinMove, ahk_pid %_PidCMDHere%,, -10, -4, %A_ScreenWidth%, %A_ScreenHeight%
    }
    Else {
        Run, "C:\Windows\System32\cmd.exe" /T:0A /D /K PROMPT $P$G$_$G & PUSHD "C:\Windows\System32\",,, CMDHerePID
        WinWaitActive, ahk_pid %_PidCMDHere%
        WinMove, ahk_pid %_PidCMDHere%,, -10, -4, %A_ScreenWidth%, %A_ScreenHeight%
    }
}
Return