Windows script to copy some text to the clipboard?

27

5

I am using an application that requires several attempts to log in (because of overloaded servers).

This app has no "remember my password" feature.

Therefore, I would like to make a script (preferably a .bat script), that would first copy my password into the clipboard -so that I don't have to retype my password on every log on attempt- , then launch the application (easy part)

Is this possible with a MS-DOS command ? Do I need a little exe or another script language ?

I'm obviously looking for the quickest solution to implement.

Thanks in advance for your ideas

Sébastien

Posted 2011-01-09T17:41:17.200

Reputation: 827

I don't think you can solve this in a .bat file, have a look at autohotkey it should fix your problem in a few lines, if you already know a programming language this should be fairly easy. – Guillermo Siliceo Trueba – 2011-01-09T17:51:07.123

Answers

34

http://www.petri.co.il/software/clip.zip
Note- Petri's link is currently down. He got it from windows server 2003 but I see clip.exe on windows 7 too. It's on windows versions post windows 7 too.

C:\>echo abc| clip  <-- copies abc to the clipboard.

EDIT
The main thing is that clip command but as pointed out by Asu, a line like echo abc will also send a \r\n (which is a new line). If you want to avoid that, then that's a very standard issue solved by replacing echo texttoecho, with echo|set/p=texttoecho So C:\>echo|set/p=texttoecho|clip

further addition
You can of course then paste with right click, but for a command line paste too.

unxutils(an ancient thing not maintained for well over a decade) has gclip and pclip (they don't seem to be in gnuwin32), with those you can copy and paste via command line.

note- gnuwin32 might not be that updated either.

C:\unxutilsblah\usr\local\wbin>echo a|gclip <-- copy a to clipboard

C:\unxutilsblah\usr\local\wbin>pclip  
a

C:\unxutilsblah\usr\local\wbin>

note- you can just copy all of wbin to e.g. c:\unxutils, and the EXEs have no dependencies/dlls.

and you can of course do pclip>a.a to paste to a file. or pclip|somecmd

C:\>(echo b & echo a)<ENTER>
b
a

C:\>

C:\unxutils>(echo b & echo a)|gclip<ENTER>


C:\unxutils>pclip<ENTER>
b
a

C:\unxutils>pclip|sort<ENTER>
a
b

C:\unxutils>

barlop

Posted 2011-01-09T17:41:17.200

Reputation: 18 677

also that link mentions the raymond chen blogpost but also a comment on the answer there on this SO link mentions a comment on the raymond chen post that uses unxutils gclip and pclip http://stackoverflow.com/questions/1704455/pass-clipboard-to-batch-in-windows-xp

– barlop – 2015-07-28T20:18:59.680

Unfortunately the http://www.petri.co.il/software/clip.zip link no longer seems to work.

– Bill Rodman – 2016-03-25T15:28:07.027

2However I see that clip.exe is available under Windows 10. – Bill Rodman – 2016-03-25T15:34:56.607

The set/p solution still add a blank space at the end. Any perfect solution? – Thierry Dalon – 2017-06-26T12:39:48.010

@ThierryDalon no it doesn't.. to prove it, i'll use the xxd command which is bundled with vim7 (i'd recommend getting that command). echo|set/p=a|xxd -p it shows the hex of the output of set/p=a, and that line echo|set/p=a|xxd -p outputs 61 it doesn't output 6120. (20 is hex for space). If you put a space after the 'a' and before that second pipe. So if you did echo|set/p=a |xxd -p then you get 6120. – barlop – 2017-06-26T22:34:54.823

@barlop I am talking about the string in the clipboard after piping to clip. For me it has a space at the end. (I see it when I paste in notepad the cursor is one char after the end of the string) – Thierry Dalon – 2017-06-28T19:44:21.103

@ThierryDalon if you use some command line tools it is easier to troubleshoot. See http://i.imgur.com/3Ex5s4c.png that all works as shown when using those tools or pasting in command line or in notepad. (I did find one time I tried adding round brackets that I got a space and in another case maybe a new line, so i'm not using round brackets) and the cursor was not a space after 'c' in that notepad screenshot. See what results you get in the command line first. It may turn out you have a funny version of a command. But see what results you get on the command line with those commands

– barlop – 2017-06-28T22:13:16.277

On a related note, in the cmd window, left click the top left, then properties, then in the options tab, check the box that says quickedit mode. Now you can copy/paste fast. click hold and drag with the mouse over what you want to copy selecting it, press ENTER to copy. Right click to paste. – barlop – 2011-01-09T22:49:52.100

and to paste to the command line, right mouse click. to get it within a batch file, you will need to write a tiny script or executable in some other language like vbs, perl, c to do it or compile somebody elses http://stackoverflow.com/questions/1704455/how-to-pass-clipboard-to-batch-in-xp btw, .vbs uses wshextra.dll set clip = createobject("WshExtra.Clipboard") Looks like Perl does it in two tiny lines and needs no extra file.

– barlop – 2012-03-22T18:13:33.507

and the webpage on petri's site that mentioned it though many others just link to the file. http://www.petri.co.il/quickly_copy_error_and_display_messages.htm

– barlop – 2012-12-12T15:29:17.360

26

barlop's option isn't entirely correct because echo will add a newline character to your password breaking it.

What you need to use instead is this:

echo|set /p=MyPassWord|clip

This way the string will be copied to the clipboard as is.

Asu

Posted 2011-01-09T17:41:17.200

Reputation: 463

not really as is: a blank space is appended at the end. – Thierry Dalon – 2017-06-26T12:41:24.897

@ThierryDalon "|clip" at the end removes the space, so you should not have that appended. – Asu – 2017-07-03T15:31:27.970

1This should be an accepted answer. Helped me with new line character and quotation marks. – Piotr Sobczyk – 2013-09-17T15:16:01.543

should have just edited the existing answer – endolith – 2014-05-24T03:20:49.413

@endolith I didn't have the privileges to do that at the time. – Asu – 2014-05-25T18:29:45.330

1

AutoIt v3 can automate windows, which makes trying several login attempts easy.

AutoIt v3 is a freeware BASIC-like scripting language designed for automating the Windows GUI and general scripting. It uses a combination of simulated keystrokes, mouse movement and window/control manipulation in order to automate tasks in a way not possible or reliable with other languages (e.g. VBScript and SendKeys). AutoIt is also very small, self-contained and will run on all versions of Windows out-of-the-box with no annoying "runtimes" required!

AutoIt was initially designed for PC "roll out" situations to reliably automate and configure thousands of PCs. Over time it has become a powerful language that supports complex expressions, user functions, loops and everything else that veteran scripters would expect.

They have good examples, documentation and a solid community that can help you with script problems.

Although, you might be better off asking if they could solve the problem with their overloaded servers, as automating requests might only make the problem worse for them...

Tamara Wijsman

Posted 2011-01-09T17:41:17.200

Reputation: 54 163

This tools looks very useful, however I will stick to the manual Ctrl - V, it won't take me much longer now that I don't have to type the password or manually Ctrl-C it. – Sébastien – 2011-01-09T22:30:55.213

1@Sebastien: I don't understand, you're asking for a script to help you fill in the password more easily and yet just accept a solution that just does a copy/paste? Did you know that if you select the password circles in the password box that you can just copy/paste them from there? Please note that you can vote answers up with the arrow above the number to reward reputation. – Tamara Wijsman – 2011-01-09T23:22:23.780

1The first answer actually exactly fulfills the need (pretty basic) I described in the question. Now when I want to take the automation a step further, I'll definitely take a look at AutoIt ;) [and about the 'vote up', I don't have the reputation for it yet] – Sébastien – 2011-01-10T08:56:31.523

"password circles" Do you mean asterisks? They're not circles! Try typing them with a huge font in Ms Word ;-) Err, well, I guess some software uses circles! – barlop – 2011-01-11T23:10:15.950

@barlop: No, not asterisks. Circles are indeed wrong, I think they are called "dots", see this picture. Next time, I'll type "password characters" which should keep me safe until someone invents "password figures"...

– Tamara Wijsman – 2011-01-11T23:18:39.680

1

I've myself encountered a similar scenario and here's how I've solved it.

First, I store my passwords in the Windows Credential Vault (Windows Vista and greater). For this, I use Python's keyring library, but you can just as well use something like CredMan (Powershell) to manage them. Using the Windows Credential Vault means the password never has to be typed on the command line, so is unlikely to leak (such as through a command-line history).

Second, I use a tool like clip to copy the password to the clipboard.

You may find you want to combine the two with your own PowerShell script that grabs the text from the credential manager and puts it on the clipboard. The script could be something as simple as:

$cred = Read-Creds 'Some System'
[Windows.Forms.Clipboard]::SetText($cred.CredentialBlob)

Then, all you have to do is add the password to 'Some System' in Windows Credential Manager and that script will magically put the password on the clipboard on command.

Jason R. Coombs

Posted 2011-01-09T17:41:17.200

Reputation: 1 952

1Thank you; I was going to suggest using Powershell to access the clipboard. Note that you can use powershell -c <command> from within CMD, so if it actually has to be a batch file, it could use something like powershell -c [Windows.Forms.Clipboard]::SetText(%PASSWORD%) after CMD was used to load the password into the (temporary, please!) %PASSWORD% variable. – CBHacking – 2016-09-26T18:48:39.770

0

The easies way to do this is:

  1. open Notepad
  2. copy [CTRL + C] this line:
  3. ECHO | SET /P=mypassword | CLIP
  4. paste [CTRL + V] this line to Notepad
  5. and change "mypassword" for your text, then save it as "filename".bat

And you can edit this copied text whenever you want just edit this file with Notepad.

user461284

Posted 2011-01-09T17:41:17.200

Reputation: 17

-1 you should have summarized this answer in one sentence at the top, before your baby steps (if you include baby steps at all), and that summary would be to save people that are technical from having to read all of your baby steps. The questioner knows how to write a script so he knows that ctrl-c copies to clipboard and ctrl-v pastes. as would most super users , which is who this site is meant for.. Of course, your answer is already satted by others. You've just added baby steps that the questioner has no need for. – barlop – 2016-01-14T05:21:00.583

1And you should state that you are not offering a new solution here, just elaborating on one already stated. – barlop – 2016-01-14T05:24:19.123

Why repeat someone elses answer? – Asfand Qazi – 2016-10-19T11:41:15.570