Friday Fun Out-Rainbow

For my readers who are just discovering my Friday posts, let me remind you that these are not necessarily practical, production worthy PowerShell scripts and functions. They are meant to be fun, yet educational. For example, in today’s Friday Fun I have a function that takes string input and writes colored output to the console. So while you may not need the function itself, you might learn something about writing advanced functions, arrays, Write Host and more. So today is all about rainbows, unicorns and puppies. Continue reading “Friday Fun Out-Rainbow”

Out-Clip

I’ve started working on the 2nd edition of Managing Active Directory with Windows PowerShell: TFM. As with almost all of my writing projects it will be full of PowerShell code examples. In the past I’ve always relied on a manual copy and paste to add content to the manuscript. The PowerShell Community Extensions made this a little easier in the past with their Out-Clipboard cmdlet. That made it easier to grab the results so I could paste them in my Word document. But I also needed the PowerShell command which meant another copy and paste. What I really needed was a way to grab the expression AND the result and send both to the Windows clipboard. Thus was born Out-Clip.














































































































































































































































The function requires the Write-Clipboard cmdlet from the PowerShell Community Extensions. I use a Try/Catch block to verify the cmdlet exists in your current session. If it doesn’t, you get an error message and the pipeline is terminated. You’ll need to make sure you manually load the module before running this function.

The function itself is pretty basic I think. You can specify one or more scriptblocks either with the –Scriptblock parameter or piped into the function.

PS C:\> out-clip {get-service spooler | select *}

Since I prefer almost all of my code examples to use a prompt of PS C:\>, the function prepends it before the scriptblock command. If you prefer to see the actual or current location, use the –UseCurrentLocation parameter. If you prefer no prompt, then use –NoLocation. Because these two values are mutually exclusive, I’ve used two parameter sets, which you can see when you look at help.

PS C:\> get-help out-clip

NAME

Out-Clip

SYNOPSIS

Capture a PowerShell command and its result to the Windows clipboard.

SYNTAX

Out-Clip [-ScriptBlock] <ScriptBlock[]> [-UseCurrentLocation] [<CommonParameters>]

Out-Clip [-ScriptBlock] <ScriptBlock[]> [-NoLocation] [<CommonParameters>]

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.

RELATED LINKS

Write-Clipboard

Set-Clipboard

Out-Clipboard

Get-Clipboard

REMARKS

To see the examples, type: “get-help Out-Clip -examples”.

For more information, type: “get-help Out-Clip -detailed”.

For technical information, type: “get-help Out-Clip -full”.

Out-Clip uses a hash-table to store the scriptblock command and the result. If the scriptblock fails, then the last error is grabbed and used instead for the result. Thus I always get the command and result in the clipboard, even if the command failed. After all scriptblocks have been evaluated the hash-table’s contents are piped to Write-Clipboard.














The script file also defines an alias, oc, for the function since I don’t like to type anymore than I have to.

Granted, this function meets a pretty specific need, but it does demonstrate some PowerShell 2.0 scripting features such as multiple parameter sets, Try/Catch and hash tables. If you try it out, I hope you’ll let me know what you think.

Appending Property Values in PowerShell

This morning I helped out a fellow scripter in the PowerShell forum at ScriptingAnswers.com. He was trying to figure out an Exchange 2007 problem. He wanted to update a property value, but keep the existing property values. This seems a like a reasonable request and one that isn’t limited to Exchange. There are plenty of objects you might work with in PowerShell where you want to keep the existing property value and add to it. My solution is specific to the Exchange problem, but I think you could use it as a model for similar problems with other objects.

Continue reading “Appending Property Values in PowerShell”