Output to clipboard

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 7 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
techMonkie
Posts: 4
Last visit: Mon Nov 14, 2016 7:32 am

Output to clipboard

Post by techMonkie »

I have a form I have been using for some time, but the servicedesk team have now asked if, rather than just outputting the results to the screen, I can have the results saved to the clipboard so they can paste the results into tickets or email.
For most of the options I have been able to do this simply by piping the results to clip, however, I have one function (see below) that I cannot seem to get to do this. Can anyone help?

The script this button runs is:

$LastLogonButton_OnClick=
{$logons=@()
[System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().DomainControllers |
ForEach{
$logonInt=(Get-Aduser -Filter "sAMAccountName -eq '$($EntryBox.text)'" -server $_.Name -properties lastlogon).lastlogon
$logons+=[datetime]::FromFileTime($logonInt)
}
$results.Text=($logons| Measure-Object -Max).Maximum
}

This checks all of the DCs and pulls the last logon for the user account. Right now it outputs the results to a window in the form ($results), I want to change this so it outputs to clipboard but I can't figure out how to do this, I'm having a bit of a blonde moment!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Output to clipboard

Post by jvierra »

Just output to clipboard:

[System.Windows.Forms.Clipboard]::SetText($results.Text)
User avatar
techMonkie
Posts: 4
Last visit: Mon Nov 14, 2016 7:32 am

Re: Output to clipboard

Post by techMonkie »

thanks, but where do I insert this in the script?
User avatar
techMonkie
Posts: 4
Last visit: Mon Nov 14, 2016 7:32 am

Re: Output to clipboard

Post by techMonkie »

Ok, I have tried editing the script using the suggested code above, and now I get no output. Nothing is saved to the clipboard or screen and no errors are reported.
I don't know where to put the code you provided, I am not a powershell expert, I know some but not enough to be anything but a novice so any help is appreciated

Where in this script should I put the line provided - [System.Windows.Forms.Clipboard]::SetText($results.Text) ?

Original script:

$LastLogonButton_OnClick=
{$logons=@()
[System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().DomainControllers |
ForEach{
$logonInt=(Get-Aduser -Filter "sAMAccountName -eq '$($EntryBox.text)'" -server $_.Name -properties lastlogon).lastlogon
$logons+=[datetime]::FromFileTime($logonInt)
}
$results.Text=($logons| Measure-Object -Max).Maximum
}
DevinL
Posts: 1098
Last visit: Tue Jun 06, 2017 9:15 am

Re: Output to clipboard

Post by DevinL »

For future reference, please place code between code blocks to make it easier for us to read your code and help you figure out the issue. To do this simply click the code box just above where you type out your reply:
Code_Selection.png
Code_Selection.png (14.01 KiB) Viewed 3598 times
That aside, it appears as though you do your output with this line here:
  1. $results.Text = ($logons | Measure-Object -Maximum).Maximum
If you wish to keep the output in the $results control as well as your clipboard, you should be able to add the clipboard line just below the previously mentioned line and it'll also be placed on your clipboard. Your ending event would look similar to this:
  1. $LastLogonButton_OnClick = {
  2.     $logons = @()
  3.     [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().DomainControllers |
  4.     ForEach-Object{
  5.         $logonInt = (Get-Aduser -Filter "sAMAccountName -eq '$($EntryBox.text)'" -server $_.Name -properties lastlogon).lastlogon
  6.         $logons += [datetime]::FromFileTime($logonInt)
  7.     }
  8.     $results.Text = ($logons | Measure-Object -Maximum).Maximum
  9.     [System.Windows.Forms.Clipboard]::SetText($results.Text)
  10. }
DevinL
SAPIEN Technologies, Inc.
User avatar
OldLost
Posts: 55
Last visit: Wed Feb 21, 2018 8:00 am

Re: Output to clipboard

Post by OldLost »

I've been using the following function for a few years now and its always worked for me.
  1. function Out-Clipboard {
  2.     <#
  3.     .SYNOPSIS
  4.     Sends the given input to the Windows clipboard.
  5.     .EXAMPLE
  6.     dir | Out-Clipboard
  7.     This example sends the view of a directory listing to the clipboard
  8.     .EXAMPLE
  9.     Out-Clipboard "Hello World"
  10.     This example sets the clipboard to the string, "Hello World".
  11.     #>
  12.     [CmdletBinding()]   [OutputType()]
  13.     Param(
  14.         [Parameter(ValueFromPipeline)] [PSObject] $InputObject
  15.     )
  16.     BEGIN { $ObjectsToProcess = @() }
  17.  
  18.     PROCESS { $ObjectsToProcess += $InputObject }
  19.  
  20.     END {
  21.         ## Launch a new instance of PowerShell in STA mode.
  22.         ## This lets us interact with the Windows clipboard.
  23.         $ObjectsToProcess | PowerShell -NoProfile -STA -Command {
  24.             Add-Type -Assembly PresentationCore
  25.  
  26.             ## Convert the input objects to a string representation
  27.             $clipText = ($Input | Out-String -Stream) -join [environment]::NewLine
  28.  
  29.             ## And finally set the clipboard text
  30.             [Windows.Clipboard]::SetText($clipText)
  31.         }
  32.     }
  33. } # Out-Clipboard
HTH
User avatar
techMonkie
Posts: 4
Last visit: Mon Nov 14, 2016 7:32 am

Re: Output to clipboard

Post by techMonkie »

Thank you both, I now have a working script.
The function looks like it will be useful for something else.

Much obliged.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Output to clipboard

Post by jvierra »

This does everything that the function does in one line:

[System.Windows.Forms.Clipboard]::SetText($results.Text)
User avatar
OldLost
Posts: 55
Last visit: Wed Feb 21, 2018 8:00 am

Re: Output to clipboard

Post by OldLost »

I disagree. It doesn't handle pipeline input, for one.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Output to clipboard

Post by jvierra »

Pipeline input is not necessary. To pipeline simply add it to a pipeline.

You cannot ever use the clipboard in a pipeline. It can only handle on item at a time. Writing a second item removes the first item. Your code just accumulates the input for a single write.

The question was how to transfer the contents of one textbox to the clipboard. While you function is not wrong but it is unnecessary in this particular case as one line is all that is needed.
This topic is 7 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked