PowerShell 5 introduces cmdlets to copy text to the clipboard, and paste it back: Set-Clipboard and Get-Clipboard.

Set-Clipboard also supports the –Append parameter that enables you to add more text to the clipboard. This can be an unconventional yet useful way of logging what a script does:

Set-ClipBoard "Starting at $(Get-Date)"
1..30 |
  ForEach-Object {
    Set-ClipBoard -Append "Iteration $_"
    $wait = Get-Random -Minimum 1 -Maximum 5
    Set-ClipBoard -Append "Waiting $wait seconds"
    Start-Sleep -Seconds $wait
    "Processing $_"

  }

The dummy script uses Set-Clipboard to paste information to the clipboard. While the script runs, you can open notepad and paste the clipboard content to get a snapshot of the script logging data.

Twitter This Tip! ReTweet this Tip!

Anonymous