2

I have the following batch file script that I trying to run that will fetch my password to my clipboard. I have tried multiple different methods but I can't seem to get it to work.

set "passwd=p455code&"
echo %passwd%|clip

When I run it I get the following error:

C:\aliases>set "passwd=p455code&"

| was unexpected at this time.

Please let me know what I am doing wrong here.

2 Answers2

1

Please let me know what I am doing wrong here.

Forget using a variable for this, you don't need one.

The following 1 liner works:

echo|set/p="p455code&"|clip

Source: Windows script to copy some text to the clipboard?, answer by barlop

DavidPostill
  • 162,382
0

You are using the operator & without ^escaping and send this "expected output" to pipe direct to clip command, and because the cmd.exe is interpreting as run command & | command you get the error:

| was unexpected at this time.

Also, instead of expanding the value/strings saved in the variable to copy to the clipboard, use a listing of it and capture the value in a for /f loop:

set "_pwd=p455code^&"
for /f tokens^=2delims^=^= %i in ('set _pwd')do echo\%i|clip

  • Invoking powershell in cmd:
powershell -nop -c "'p455code&'|clip"
  • In PowerShell
'p455code&'|clip

Io-oI
  • 9,237