I can copy a string to the clipboard using a pipe and calling the clip program, such as | clip.exe, but how can I paste at the current caret location? All I need is to paste a value at the current location. Thank you for your help!
Why do you want to do that?
Maybe you could use the get and set clipboard.
"test"|Set-Clipboard Get-Clipboard
Neither of these are recognized by my Powershell. I am using Powershell V2 and unfortunately I can’t upgrade. Ultimately I am trying to read a COM port then paste the result at the current location in Excel or Notepad, etc. Everything else is working, just this last thing is holding me up. Thank you for your help!
PSv2:
function Get-ClipBoard { Add-Type -AssemblyName System.Windows.Forms $tb = New-Object System.Windows.Forms.TextBox $tb.Multiline = $true $tb.Paste() $tb.Text } function Set-ClipBoard { param ( [String][Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)] $Text ) Add-Type -AssemblyName System.Windows.Forms $tb = New-Object System.Windows.Forms.TextBox $tb.Multiline = $true $tb.Text = $text $tb.SelectAll() $tb.Copy() }
Thank you for your help! This is really close, but it pastes in the command window instead of caret location. Hopefully I’m using it right. Here is the full script I’m using to test these functions.
function Get-ClipBoard { Add-Type -AssemblyName System.Windows.Forms $tb = New-Object System.Windows.Forms.TextBox $tb.Multiline = $true $tb.Paste() $tb.Text } function Set-ClipBoard { param ( [String][Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)] $Text ) Add-Type -AssemblyName System.Windows.Forms $tb = New-Object System.Windows.Forms.TextBox $tb.Multiline = $true $tb.Text = $text $tb.SelectAll() $tb.Copy() } Set-Clipboard("Testing again: 1, 2, 3...") Start-Sleep -s 2 Get-Clipboard Get-Clipboard Get-Clipboard Write-Host "Done!"
During the 2 second delay I change cursor location to Notepad but the clipboard contents paste into the command window. Thanks again!
Ok, I found a work around. The best way for me to paste at the cursor location is to send the CTRL+v keys directly. Here is a MWE. Thank you again for your help!
# Load System.Windows.Forms to use SendKeys [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") # Copy some text to the clipboard "Testing again: 1, 2, 3..." | clip.exe # Delay for 2 seconds while moving the cursor Start-Sleep -s 2 # Paste using CTRL+v 3 times just for verification [System.Windows.Forms.SendKeys]::SendWait("^{v}") [System.Windows.Forms.SendKeys]::SendWait("^{v}") [System.Windows.Forms.SendKeys]::SendWait("^{v}") # Finish up with the command window Write-Host "Done!" Read-Host "Press enter to close this window..."
If you feel yourself capable to understand this topic
http://stackoverflow.com/questions/2556872/how-to-set-foreground-window-from-powershell-event-subscriber-action
you can select notepad window automatically
and there is alot of modules and examples about direct working with excel thru COM
Thank you for the reference! I’ll check it out.