Customize Your PowerShell, Everywhere

PowerShellForgive me as I sneak onto a SharePoint blog to talk about another great Microsoft administrative tool…

I use Windows PowerShell all day every day, and as such, I’ve put a lot of time and effort into customizing it to meet my needs. I’ve tweaked the command prompt to show me git status.  I even wrote a module called project-commands to simplify building and testing our many different web site projects.

Bottom line: I like my PowerShell, and I like it my way.

In my role as the lone code slinging developer here at Fpweb.net, I do a small bit of maintenance on a handful of servers. Mostly I set up web sites and othe rbasic IIS administration.  My preference is, of course, to do it from the command line.  A while back I found ‘appcmd’, which is the command line configuration tool for IIS.  A quick one liner…

appcmd add site /name:"iris" /bindings:"http/*:80:www.fpweb.net"  /physicalPath:"C:\inetpub\wwwroot\iris\v42"

…and we’ve spun up a web app, bound to www.fpweb.net.  It’s as easy as that.  Except of course it’s not.

appcmd.exe is never in the path by default, and I am invariably greeted with the dreaded ‘The term appcmd is not recognized…’.  Rather than add it to the path (which would have the side effect of adding all the other executables that might be in the same directory), my preference is to create an alias directly to appcmd.exe itself.  Like so:

Set-Alias appcmd "$env:systemroot\system32\inetsrv\appcmd.exe"<\code>

And now we can happily appcmd away. Notice the use of $env:systemroot. This is better than literal paths since locations can change across Windows versions or at the whim of the original installer.

Of course, I don’t want to type this customization every time I log onto a box. And this is just one customization, I have many.

At this point we’d likely reach for PowerShell profiles. Normally that would be a fine solution. However this isn’t my primary box. I’d need to repeat each customization to the profile of each box I find myself RDP’d into. That’s no good. Plus I don’t want these changes hanging around. I want them to disappear without a trace when I log off.

Here is a solution that covers all these bases. I still create the custom PowerShell profile file. Rather than copy it to every machine (and then deal with updating later), I load it on the fly, using a command like this:

(new-object Net.WebClient).DownloadString("https://raw.github.com/jbuedel/dotfiles/master/adhoc-profile.ps1") | iex<\code>

See, I’ve placed my profile .ps1 file on an accessible website.  I then download and execute it on the fly by piping the profile text right into invoke-expression, never writing the file to disk.  Once I quit that PowerShell session, all my tweaks disappear with it. I need only commit the one line to memory.

I like github for hosting the profile because then I get all the change control git offers.  Should my profile contain secrets (not passwords of course, but perhaps other sensitive info), I have the option of switching to a private github repo. The file then requires authentication for access.

Of course any web site you have access to place a file on will work.  Dropbox for instance, or SharePoint even.

This is certainly not a technique I came up with.  Chocolately (a Windows package management solution) uses this to install the installer.  As does ps-get, a PowerShell specific package manager.   It’s a nice fit for this “I’m on a random server, where’s my stuff!?” problem, and I hope it helps you out!

About Fpweb.net Crew

Our business is centered on bringing enterprise-class strategy, support, and security to your hosted or managed platforms no matter where you choose to deploy your environment. We specialize in providing managed services, cyber security, and expert, USA-based, 24/7 Absolute Support® on-premises, or in any cloud.
This entry was posted in PowerShell and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

Let's make sure you're human first: *