Set-Clipboard.ps1 by Lee Holmes 17 months ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/2219"></script>download | new post
From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes
- #############################################################################
- ##
- ## Set-Clipboard
- ##
- ## From Windows PowerShell Cookbook (O'Reilly)
- ## by Lee Holmes (http://www.leeholmes.com/guide)
- ##
- ##############################################################################
- <#
- .SYNOPSIS
- Sends the given input to the Windows clipboard.
- .EXAMPLE
- dir | Set-Clipboard
- This example sends the view of a directory listing to the clipboard
- .EXAMPLE
- Set-Clipboard "Hello World"
- This example sets the clipboard to the string, "Hello World".
- #>
- param(
- ## The input to send to the clipboard
- [Parameter(ValueFromPipeline = $true)]
- [object[]] $InputObject
- )
- begin
- {
- Set-StrictMode -Version Latest
- $objectsToProcess = @()
- }
- process
- {
- ## Collect everything sent to the script either through
- ## pipeline input, or direct input.
- $objectsToProcess += $inputObject
- }
- end
- {
- ## Launch a new instance of PowerShell in STA mode.
- ## This lets us interact with the Windows clipboard.
- $objectsToProcess | PowerShell -NoProfile -STA -Command {
- Add-Type -Assembly PresentationCore
- ## Convert the input objects to a string representation
- $clipText = ($input | Out-String -Stream) -join "`r`n"
- ## And finally set the clipboard text
- [Windows.Clipboard]::SetText($clipText)
- }
- }
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.
PowerShell Code Repository