PowerShell Profile on Roaming Profile

Posted Wednesday, June 27, 2007 2:39 PM by cromwellryan

NOTE: Impatience just leads to frustration.

When I first starting playing with PowerShell here at the office, I was all excited to start adding little cmdlets to my personal profile and add to my stellar productivity (tongue firmly planted in check).  This would hopefully reduce the net loss incurred by my learning PowerShell in the first place. 

So I took the first step and created my empty profile:

ps> notepad $profile

and added a nice little cmdlet to send email.  This will replace that exe tool I've always used to send emails in batch files or just whenever I need to at the command prompt.

$defaultSendMailHost = "emcrs71"
$defaultSendMailFrom = email@email.com

function send-mail( [string] $to, [string] $subject, [string] $body )
{
     $smtp = new-object System.Net.Mail.SmtpClient
     $smtp.Host = $defaultSendMailHost
     $email = new-object System.Net.Mail.MailMessage
     $email.From = $defaultSendMailFrom
     $email.To.Add( $to )
     $email.Subject = $subject
     $email.Body = $body
     $smtp.Send( $email )
}

Start it up again and WHAM... 

File \\<server>\<username>\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 cannot be loaded. The file \\<server>\<username>\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 is not digitally signed. The script will not execute on the system. Please see "get-help about_signing" for more details.

Sign my scripts?!  How dare you!  I was too annoyed to deal with it and took the ever so appalling step of changing the PowerShell Short cut to point to a local folder on my C drive.  Please don't stop reading as I've learned from my mistakes and have rectified my ways.

A few weeks later I ran across the PowerShell Community Extensions, which I highly recommend PowerShell-er install and, in fact, replaced my send-mail cmdlet above.  PSCX has an option to install it's own profile which I chose, but after that I started receiving the Digital Signing guff. 

In the end, I did what any good PowerShell-er with a growing Profile should do.  I created a cmdlet to sign my profile.  Now I can open PS, type edit-profile (or use my ep alias), save it, and hit sign-profile (sp alias) and I'm good to go.  Here it is:

function edit-profile()
{
    notepad $profile
}

function sign-profile()
{
    dir $Profiledir\*.ps1 | foreach-object { sign-script $_.FullName }
}

function sign-script( $scriptsource )
{
    get-item $scriptsource
    Set-AuthenticodeSignature $scriptsource @(Get-ChildItem cert:\CurrentUser\My -codesigning)[0]
}

set-alias sm Send-SmtpMail
set-alias ep edit-profile
set-alias sp sign-profile

Now there is a little part I left out and that's creating yourself a certificate, but Scott Hanselman has a very good post that will walk you through this.

Technorati Profile
Filed under:

Leave a Comment

(required) 
(required) 
(optional)
(required)