Nicolas Formichella
Nicolas Formichella

Reputation: 127

Any possibilities to print bare environment variables in Powershell?

I regularly use the the echo $PWD > /dev/clipboard in Git Bash to hardcode paths in my scripts but I find it quite annoying to have to run Git Bash to do this, I would really like to do that in powershell

The issue it that it seems that the output of echo $PWD (or really any environnement variables) makes weird thing with the clipboard

echo $pwd

outputs

Path
----
C:\ProgramData\chocolatey\bin

And if I try to put it in clipboard using echo $pwd | Set-Clipboard it just fails silently, it tries to add it but Windows straight up refuses (Office Clipboard says that it's an unsupported fornat, although it's just text)

Set-Clipboard -Value $pwd

Works but prepends the path with this annoying Path ----

Is there any way to have just the path like %cd% works in CMD but in Powershell?

Upvotes: 2

Views: 154

Answers (1)

Bassie
Bassie

Reputation: 10400

Try this

Set-Clipboard -Value $pwd.Path

Or you can use

$pwd.Path | clip

However see mklement0's comment

Note that if you pipe to clip.exe, a trailing newline is invariably added and with non-ASCII characters you'll run into encoding issues

Upvotes: 2

Related Questions