So, people keep talking about a better PowerShell script repository, and there’s at least 3 or 4 different people working on improvements, but for now, the one we have is working ok, and already has hundreds of scripts in it — but there hasn’t been a way to add or retrieve scripts from within PowerShell.

So while I was taking a break from working on my own replacement script repository ;) I whipped up some scripts to Send-Paste, Get-Paste and Find-Paste. These scripts still need some work, but I think you’ll find them useful, so I’ve posted a version that is compatible with PowerShell v1 (and works fine on the 2.0 CTP) and doesn’t need any additional scripts (it includes the latest Get-WebFile script for downloading the files).

The Find-Paste function takes a search string, and adds wildcards — but otherwise performs the same search as you can already do on the web page. It returns the results in a PowerShell friendly PSObject which by default is just displayed in the host.

In order to download a script using Get-Paste, you must know the ID of the script — which you can get from the output of Find-Paste. Note that because Find-Paste does a full-text search, you may get results that are not the script you’re looking for (if they call that script, for instance), and you may get many versions of the script (and newer versions are not necessarily at the top of the search results).

The Send-Paste script is most useful for sending things to the temporary PowerShell pastebin on my site but can be used successfully to send scripts to the permanent PowerShell Script Repository … in fact, that’s how I uploaded the PowerShell Pastebin Functions to the repository … you just have to be careful to specify the title and description when you do that. Edit: I attached the script here on my site also because I made a mistake and deleted the original copy from the repository. [new]

So, you now have something a little bit more like a proper script repository. Hopefully the Usage comments in the script will be enough to help you figure out how to use the various commands, (and I did blog about the Send-Paste script about a month ago) but just in case you need help, let me give you a simple example.

Suppose I need to find a script to encrypt text so I can store some SecureString objects (like the passwords in a PSCredential object) in a file…


[100]: Find-Paste encrypt

Id          : 116
Title       : Start-Encryption
Description : Functions to encrypt and decrypt strings using the Rijndael symmetric key algorithm
Author      : Joel Bennett
Date        : 133 days ago
Link        : http://powershellcentral.com/scripts/116

Id          : 416
Title       : Pastebin Functions
Description : Send-Paste, Get-Paste, and Find-Paste ... to let you use PowerShellCentral.com/Scripts a little bit more
              like CPAN while you wait for us to get our act together and give you something better. :)
              IMPORTANT NOTE: the Send-Paste script defaults to our *temporary* pastebin site because we do not want
              you to fill up the central pastebin site with snippets ... but the other two default to the main long-term
              script repository.
Author      : Joel Bennett
Date        : 1 hour ago
Link        : http://powershellcentral.com/scripts/416

It looks like I found two, but clearly the second one is not the right one (I mentioned this in one of my examples in the pastebin scripts, and amusingly enough, that screwed up my example). So to download the script I want, I just do:


[101]: Get-Paste 116 Start-Encryption.ps1

    Directory: Microsoft.PowerShell.Core\FileSystem::C:\Users\Joel\Documents\WindowsPowerShell\Scripts

Mode           LastWriteTime       Length Name
----           -------------       ------ ----
-a---     5/30/2008 12:23 AM     2.378 KB Start-Encryption.ps1

Simple enough, right? I could have used the -Passthru option to see the script in the console, but since I was pretty sure it was the only one that might be useful (and since using -Passthrough just to display it on the console would mean downloading it a second time, if I actually wanted it). Another option would be to use the Tee-Object cmdlet, which would let us download it, show it in the host, and also save it in the file, all at once! :)


Get-Paste 116 -Passthru | tee -file Start-Encryption.ps1

Incidentally, I have a version of this which uses my PoshHttp snapin and Get-Web cmdlet, but it’s also written as a module and therefore requires PowerShell 2.0 CTP2 — for various reasons, I’m choosing to release that one separately later. If you are using the CTP, you could take advantage of the WPF UI abilities and try piping Find-Script ... | "Select-Grid":http://huddledmasses.org/wpf-from-powershell-select-grid/ | Get-Script to pick the script you want visually. :-D

2 Responses to “Automating the PowerShell Script Repository”