How can I change my windows display user name in command prompt? - windows

I need to change my PC's cmd user name. Is there any way to do that?
Like currently in cmd I get
C:\Users\User Name>
But I want
C:\Users\User_Name>

You can do it by using the command prompt.
It would help if i could know what your username on windows is, but if we just say it is "User Name" we can craft a command.
So we start with prompt $P "$P" gives us the path so now our output is C:\Users\User Name then we can add "$H" which is a backspace. So if we add 5 of those we can delete " Name". Now our command looks like this prompt $P$H$H$H$H$H and that will give us this output C:\Users\User. Now we just have to add the "_Name". So now the command will look like this prompt $P$H$H$H$H$H_Name and we will have an output that looks like this C:\Users\User_Name. The last thing we have to add is the ">" at the end and you add that with "$G". Now we have our final command which is prompt $P$H$H$H$H$H_Name$G and will give us the output C:\Users\User_Name>. Try to see if you can craft your own command with your username.
To run the command every time you launch cmd you have to make a registry value in Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor called "autorun" (type string) with your command as the value.

Related

short path of current directory in cmd windows

I wish there's a way to show a shortened directory path in windows CMD, I searched the internet and found this command:
for %I in (.) do %~nxI
if you enter this command in cmd and press enter it would show the current dir name. suppose I was standing in my desktop it will print:
Desktop
thats enough for me. but not yet, I tried to save this command in a environment system variable then pass that variable to cmd line, but this way it would print the command itself instead of the result.
created a variable named PROMPT then value is:
%username%$s$p$s$d$t$_-$g$s
result is:
my_username full_path_to_current_dir date time newline dash greater_than_mark(>)
Mahdi c:\Users\Mahdi\Desktop 23/02/2020 19:27:38.93
->this is an screenshot of what Ihave: (https://i.stack.imgur.com/Auicf.png)
This is not a feature avaible in the standard prompt.
You potentially could hack it using echo off and doskeys,
but I would instead suggest checking out one of the many alternatives such as cmder or Zoc
Is this what you wanted?
For /F "Tokens=1*Delims=|" %I In (""%UserName%"|"%CD%"") Do #Prompt %~I$s%~nxJ$s$d$t$_-$g$s
Alternatively, if you wanted it setting to a variable, run the prompt command inside another for loop, and capture the output of that with the Set command.

How to save cmd output as file name

I'm running a command line script on multiple PC's and i'm trying to save username as a file name so i can see who's information i'm viewing later on.
In the command line script i run Whoami and i'd like to save it as "user"."file type". I'm trying to do this in a command line script because I always do it manually in command line and am trying to automate this process so I can do it faster.
If you know how to do it in a better way do share.
whoami > test.txt
tells it to go to a file and "test.txt" will be generated wherever your CMD CurDir is.
You may use Windows Environment variables %USERNAME% and possibly %USERDOMAIN% if the domain is needed.
%USERNAME% does not return the domain by itself.
Full list of standard environment variables: How-to: Windows Environment Variables
Use these in the command as needed. For example:
dir > %USERNAME%.txt
If you need the domain in there:
dir > %USERDOMAIN%_%USERNAME%.txt
(using _ to separate domain and username instead of \ since filename cannot contain \)
Remember to use >> instead of > if you don't want the file to be overwritten each time the command is run.
You may want to direct errors and standard output as needed: Redirecting error messages from Command Prompt: STDERR/STDOUT

Change Windows command prompt to show only current folder

Instead of showing
C:\Users\test_user\Documents\Folder\etc
show
\etc
or if possible limit it to a certain number
\Document\Folder\etc
If you check in help prompt /? there are two options that can either show the current drive or full path.
I would suggest to use new line option along with the Drive so that you will get more space to view/type the command using below combination.
prompt $P$_$G
With this you will be able to see the Path in the line above the prompt.
In short, can't see a simple way of doing it.
In order to change the prompt options you can use the prompt command. The configuration you're looking for isn't listed.
The available options can be viewed by
prompt /? in the command window.
https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/prompt.mspx?mfr=true
The following is a simple batch script which can set the prompt to include only the current folder. Note that it does not work on directory names with certain characters such as parenthesis and spaces. I named it cdd.bat.
#echo off
cd %1
for %%i in (%CD%) do set NEWDIR=%%~ni
PROMPT %NEWDIR%$G
Like others pointed out, you can use the command - prompt to set the text that is shown in cmd.
While you cannot dynamically set the path to just the parent folder, you can manually set it using:
prompt {text}
So in your case, you can set it as:
prompt etc\$G
This will result in:
etc\>
$G adds an arrow sign. You can refer the documentation for detailed explanation.
Here is a .ps1 file i use to do this for myself.
<#
FileName: promptPsShort.ps1
To set the prompt to the last folder name in the path:
> function prompt {$l=Get-Location; $p="$l".split("\")[-1]; "PS $p> "}
# works at cmd prompt, BUT NOT DIREECTLY from a .ps1 file.
RESEARCH
1. google: powershell 7 copy text into clipboard
[How to copy text from PowerShell](https://superuser.com/q/302032/236556)
[Set-Clipboard](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/?view=powershell-7)
2. google: powershell escape double quote
[Escaping in PowerShell](http://www.rlmueller.net/PowerShellEscape.htm)
3. google: powershell raw string
[About Quoting Rules](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-7)
4. Usage example: powershell
PS C:\flutter_beta\flutter\examples\catalog\android\app\src\main> pwd
Path
----
C:\flutter_beta\flutter\examples\catalog\android\app\src\main
PS C:\flutter_beta\flutter\examples\catalog\android\app\src\main> promptPsShort.ps1
Paste the current Clipboard contents into the Powershell Command Line and press Enter.
PS C:\flutter_beta\flutter\examples\catalog\android\app\src\main> function prompt {$l=Get-Location; $p="$l".split("\")[-1]; "PS $p> "}
PS main>
PS main>
PS main>
#>
$shortPromptCmdStr = #'
function prompt {$l=Get-Location; $p="$l".split("\")[-1]; "PS $p> "}
'#
Set-Clipboard -Value $shortPromptCmdStr
write-host "Paste the current Clipboard contents into the Powershell Command Line and press Enter."
Love and peace,
Joe

Redirection for a command in Registry

I'm trying to add a right click option to every file so that it executes a command. Seemed simple enough at first glance.
Right click option
By going in the registry editor, specifically HKCR\*\shell (which represents the context menu options that appear for every file type, hence the "*"), I added my own key as such :
HKEY_CLASSES_ROOT
*
shell
my_right_click_option
(Default) = "Click me!"
command
(Default) = my_command
Command
After this, the clickable entry appeared in the context menu instantly.
In my case, the key is called "checkmd5" and the command key's "Default" value stores the command 'my_command' to execute when clicked.
The command is :
"C:\Quite_a_long_path\md5\md5.exe" -n "%1" | clip
The -n option (an md5.exe switch) is just to remove the name of the program that gets displayed after the hash.
The %1 will be replaced by the right clicked program's path, and the double quotes are used in case any of the paths contain some whitespaces.
Sadly, all I've managed to do is execute the command (a cmd window appears and disappears, which means that the cmd has been executed) and also, just to be sure, I checked with Process Monitor (from the SysInternals Suite) the command line being executed in the small cmd window that appears and disappears and it looks like the command I put is being executed exactly as I wanted.
Nonetheless, even after appending to the command either "| clip" (to redirect output to clipboard) or "> file.txt" (to a file) [I tried using an absolute path for file too], the clipboard doesn't update, nor the file.txt gets created with the output of the command..
I can't seem to understand how to make it work..
Any help from you windows savvy will be greatly appreciated.
Thank you again for your patience, especially after getting to the end of this long post.
Turns out the only way to use the re-directions is by adding the cmd.exe /C before the command to be executed so that the command interpreter (cmd.exe) understands the redirection and not the program to be executed (aprogram.exe).
e.g:
aprogram.exe -param0 string > C:\temp\file.txt
wouldn't work, because aprogram.exe doesn't know how to manipulate redirections.
Instead it should be :
cmd.exe /C aprogram.exe -param0 string > C:\temp\file.txt

Why can't I start programs in the command line without /d? (Windows 7x64)

I am trying to create a batch script that opens a program. I am doing some testing and I can't figure this out:
If I run CMD.exe and input start /d "C:\wamp" wampmanager.exe the program opens
but
If I run CMD.exe and input start "C:\wamp\wampmanager.exe" I get "the current directory is invalid"
Now when I try to do start runas /profile /user:Administrator "C:\wamp\wampmanager.exe" I get prompted for Administrator's password, but nothing happens when I enter it.
Can someone please tell me how I can run the above command?
Because the start program's syntax expect the window title as its first quoted argument.
(see start /?). You can supply an empty string, however:
start "" "C:\wamp\wampmanager.exe"
or, if you don't need quotes to mask parts of the path, just leave them out altogether:
start C:\wamp\wampmanager.exe
start "some-text"
starts a new command window with "some-text" as the title of the window. To start a program, do not use the quotes around the argument
start program-name

Resources