These functions will allow you to easily save a set of credentials to disk so that they can be reused during an unattended session later. There are a few blog entries and newsgroup posts about this topic elsewhere, and Lee Holmes covers it in his “PowerShell Cookbook”, but nothing that I have seen actually helps you do the end-to-end of credential –> disk –> credential process. Hope you find it useful! I wrote this for my book, but as it’s of general use I thought I would share.
Update: 7/21/08 @ 11:16a
I’m changing the import conditions to work better on v1 of PowerShell. Download link has updated code.
Update: 7/21/08 @ 7:10p
Reader Steve S. pointed out to me that if you give Export-PSCredential a username, it does not work, but he expected it to. I agreed, so changed the logic a bit. I like it this way, now it works just like Get-Credential does (try get-credential “username” to see what I mean).
One last thing—big props to Brandon Shell who has written on this topic as well. In fact, I umm, er, was troubleshooting that script with him when he wrote it so I should know better. To be fair, I—ok I have no excuse. But I didn’t mean to steal any well-deserved thunder.
My script does store the username where his does not, although he says he’s got an updated one which does. Even so—the more the merrier so that you can see multiple ways to solve a problem.

These versions work for you? The export is fine but the import doesn’t recognize the type. I don’t think the import function can validate the custom type you created.
PS C:\Scripts\PoSH> (import-clixml credentials.enc.xml).psobject.typenames
System.Management.Automation.PSCustomObject
System.Object
Or am I doing something wrong?
You know, it works great for me in v2, but I get the same error in v1. If you look at the .xml file, does it have the typename? “ExportedPSCredential “. I bet it doesn’t.
Darn, that was pretty elegant. I guess I’ll take it out for now. Try this one: http://poshcode.org/472
(I’ll update the links above.)
Thanks for the update. I suspected a version issue.
[...] Rottenberg has put together a very nice set of functions for exporting and importing PSCredential to a file. The export function takes a PSCredential and serializes it to an XML file. The [...]
[...] Hal brings us a couple of scripts for saving credentials [...]