Search code examples
variablescmdcommandcommand-promptdoskey

How do I add a variable to a doskey command that will read the variable after it executes (not get the current variable)


I have this command:

doskey cd = cd /d $* ^&^& echo "You are in: "%cd%

doskey cd = cd /d $* ^&^& title %cd%

When I paste this in CMD it will set the current cd and not dynamically when I recall the cd from the doskey.

Current output:

C:\>doskey cd = cd /d $* ^&^& echo "You are in: "%cd%

C:\>cd Users
You are in: C:\

C:\Users>

Desired output:

C:\>doskey cd = cd /d $* ^&^& echo "You are in: "%cd%

C:\>cd Users
You are in: C:\Users

C:\Users> 

What I have tried:

doskey cd = cd /d $* ^&^& echo "You are in: "%%cd%

doskey cd = cd /d $* ^&^& %cd%|echo "You are in: "

doskey cd = cd /d $* ^&^& cmd /k "echo \"You are in: \"%cd%" < don't want a new cmd session!

Finally I want to add this to my register

[HKEY_CLASSES_ROOT\Directory\Background\shell\cmdmenua]
@="Open Command Prompt Here (Admin)"
"Icon"="%SystemRoot%\\system32\\cmd.exe"

[HKEY_CLASSES_ROOT\Directory\Background\shell\cmdmenua\command]
@="powershell -NoLogo  -WindowStyle Hidden -Command \"$V = '%V'; Start-Process cmd \\\"/k  $($V.Substring(0,2)) & cd \"$V\" & Title Administrator: $V & doskey cd = cd /d `$* ^&^& title `$*\\\" -Verb RunAs\" "


[HKEY_CLASSES_ROOT\Directory\shell\cmdmenua]
@="Open Command Prompt Inside (Admin)"
"Icon"="%SystemRoot%\\system32\\cmd.exe"

[HKEY_CLASSES_ROOT\Directory\shell\cmdmenua\command]
@="powershell -NoLogo  -WindowStyle Hidden -Command \"$V = '%V'; Start-Process cmd \\\"/k  $($V.Substring(0,2)) & cd \"$V\" & Title Administrator: $V & doskey cd = cd /d `$* ^&^& title `$* \\\" -Verb RunAs\" "

source: https://github.com/ScriptsAndApps/Context-REG


Solution

  • This is what I was looking for!

    (
     @echo off
     doskey cd=cd /D $* $T echo You are in: %^CD% 
    )
    

    And this is what I will use

    doskey cd=cd /D $* $T TITLE Administrator: %^CD%