2

i'm trying to use text that is inside the clipboard inside a powershell script. So what the purpose for the script, i want to be able to copy a file directory and then run the script so it uses the copied directory as a automatic destination. So my idea was to do something like this:

Copy-Item C:\Users\gif.gif -Destination "Copied Directory"

I'm very new to powershell scripting, so an explenation of what is going on would be nice but not needed. I origionlly thought that this could work but nope XD, this should have been a simple project but yeah it wasn't meant to be easy.

Copy-Item C:\Users\J.J\Documents\TouchPortal/Make_30fps_gif.bat -Destination | Get-Clipboard

Would love to get some help with this, and thank you in advance!

Aqua
  • 23
  • 1
  • 3
  • 2
    `$test = get-clipboard; Copy-Item C:\Users\J.J\Documents\TouchPortal/Make_30fps_gif.bat -Destination $test`? Your attempt is not how piping works. – Nico Nekoru Jun 14 '20 at 18:13

2 Answers2

3

In PowerShell core including versions 6 & 7 Get-Clipboard only works with text. If you use it after copying a folder it will return null.

In PowerShell 5.1 (Windows PowerShell) you can use the -Format parameter with Get-Clipboard

See mklement0's answer for a better description and example using -Format.

If you need to use the newer versions, you can use the shift + context menu choice > Copy as Path to get the folder's string path on to the clipboard, but that will quote the path. The quoted path will then be rejected by Copy-Item.

However, you could quickly replace the quotes like below.

Copy-Item 'C:\temp\BaseFile.txt' -Destination (Get-Clipboard).Replace('"',"")

Caution though, this seems hazardous, and I wouldn't advise it. I use Get-Clipboard all the time to get data into a console session and can attest that it's too easy to make mistakes. The clipboard is so transient, and it's use so ubiquitous that even if you make this work it's bound to burn you at some point.

Maybe you can elaborate on what you're trying to do and why. Then we can brainstorm the best approach.

Steven
  • 6,817
  • 1
  • 14
  • 14
  • Good answer, but note that Windows PowerShell _does_ allow you to retrieve data other than text, including files and directories, namely via the `-Format` parameter. Unfortunately, that is no longer supported in PowerShell [Core]. – mklement0 Jun 14 '20 at 19:49
  • thank you that is perfect! also thank you for explaning. so what i'm trying to do is this; i have the program Imagemagick that can convert a png sequence with one line of code if i have the folder where to png sequence is located. i used to do this with commend prompt by first setting the directory and then this line of code: `convert *.png -delay 1x30 -loop 0 gif.gif`. then i did the same with moving a batch file with this code. But i just thought of doing it entirely with powershell by using cmd. but i wouldn't know how to implement that with the directory yet. – Aqua Jun 14 '20 at 20:13
  • Ok, i blew myself away, so i have been using powershell for just 3 hours or so and i think this is the simplest way of doing what i wanted. apperently imagemagick also works with powershell as a input so that made everything a lot easier. `cd (Get-Clipboard).Replace('"',"") convert *.png -delay 1x30 -loop 0 gif.gif` now combined with a program that can run powershell script (touch portal, amazing program especially for programmers) it makes things incredebly easy! – Aqua Jun 14 '20 at 20:41
3

To complement Steven's helpful answer:

  • In Windows PowerShell only, you can use Get-Clipboard's -Format parameter to request clipboard data other than text.

  • In PowerShell [Core, v6+], this is no longer supported, and text is indeed the only data type supported by Get-Clipboard.


In Windows PowerShell, if you've used File Explorer to copy a directory to the clipboard (using the regular Copy shortcut-menu command or the Ctrl+C keyboard shortcut), you can access it as System.IO.DirectoryInfo instance by passing -Format FileDropList to Get-Clipboard.

  • Note: -Format FileDropList returns a collection of file-system-info objects, so as to also support multiple files/directories having been copied to the clipboard; thanks to member-access enumeration, however, you can treat this collection like a single object - assuming truly only one file or directory was copied.
# Note: Windows PowerShell only.
# The .FullName property returns the full directory path.
Copy-Item C:\Users\gif.gif -Destination (Get-Clipboard -Format FileDropList).FullName

In PowerShell [Core, v6+] you'll indeed have to use the Shift+right-click method and select Copy as path from the shortcut menu, in order to ensure that a file/directory is copied as a (double-quoted, full) path string, as shown in Steven's answer.

mklement0
  • 382,024
  • 64
  • 607
  • 775