#requires -version 2.0 # Jeffery Hicks # http://jdhitsolutions.com/blog # follow on Twitter: http://twitter.com/JeffHicks # # Learn more Windows PowerShell with a copy of Windows PowerShell 2.0: TFM (SAPIEN Press 2010) # # "Those who forget to script are doomed to repeat their work." # **************************************************************** # * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED * # * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF * # * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, * # * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. * # **************************************************************** #This function requires the Write-Clipboard cmdlet #from the PowerShell Community Extensions Function Out-Clip { <# .Synopsis Capture a PowerShell command and its result to the Windows clipboard. .Description Using Write-Clipboard from the PowerShell Community Extensions, this function will take a Windows PowerShell scriptblock, execute it and write both the command and the results to the Windows clipboard. You can also pipe a collection of scriptblocks to this function or use a comma separated list with the -Scriptblock parameter. By default the output will include the PS prompt set to PS C:\>. If you want to use the actual current location then specify -UseCurrentLocation. If you prefer no prompt at all then use -NoLocation. .Parameter ScriptBlock The scriptblock command you wish to execute. .Parameter UseCurrentLocation Use the current location for the prompt instead of PS C:\> .Parameter NoLocation Do not include any prompt information. Only display the script block and results. .Example PS C:\> Out-Clip {get-process | where {$_.ws -gt 50MB}} .Example PS C:\Users\Jeff\Documents\> Out-Clip {dir -recurse -filter *.doc} -UseCurrentLocation .Example PS C:\Work\> {get-process},{get-service} | Out-Clip -nolocation .Inputs A scriptblock .Outputs None .Link Write-Clipboard Set-Clipboard Out-Clipboard Get-Clipboard .Notes Version : 1.0 Name : Out-Clip Last Update: 7/6/2010 URL : http://jdhitsolutions.com/blog #> [cmdletBinding(DefaultParameterSetName="Location")] Param( [Parameter(Position=0,Mandatory=$True,ValueFromPipeline=$True, HelpMessage="Enter a PowerShell expression")] [ValidateNotNullorEmpty()] [scriptblock[]]$ScriptBlock, [Parameter(ParameterSetName="Location")] [switch]$UseCurrentLocation, [Parameter(ParameterSetName="NoLocation")] [switch]$NoLocation ) Begin { Write-Verbose "Starting $($myinvocation.mycommand)" Try { Write-Verbose "Verifying Write-Clipboard" Get-Command -Name "Write-Clipboard" -ErrorAction "Stop" | Out-Null } Catch { Write-Warning "Failed to find Write-Clipboard from the PowerShell Community Extensions." Break } #define prompt to display, if any if ($NoLocation) { Write-Verbose "NoPrompt specified" $myPrompt="" } elseif ($UseCurrentLocation) { Write-Verbose "Using current location $($pwd)" $myPrompt="PS $pwd\> " } else { Write-Verbose "Using default prompt" $myPrompt="PS C:\> " } #initialize an array to hold results Write-Verbose "Initializing output array" $output=@{} } #close Begin Process { foreach ($sb in $scriptblock) { #run the command and save the result Write-Verbose "Executing scriptblock" Write-Verbose $sb.ToString() $result=&$sb #check if the command succeeded. If not, then get the #last exception and make it the result if (-not $result) { Write-Verbose "Adding exception" $result=$error[0] } Write-Verbose "Building data and adding to hash table" #define the first line with the prompt and command $commandline="{0}{1}" -f $myPrompt,$sb.ToString() #add the command and results to the hash table $output.Add($commandline,($result | out-string)) } #foreach $sb in $scriptblockl } #close Process End { Write-Verbose "Writing output to the clipboard" #enumerate the keys and then the command results $output.keys | foreach {$_,($output.item($_) | out-string)} | write-clipboard Write-Verbose "Command and results written to the clipboard." Write-Verbose "Ending $($myinvocation.mycommand)" } #close end } #end function #define an alias Set-Alias -Name oc -Value Out-Clip