The Surly Admin

Father, husband, IT Pro, cancer survivor

Making a Consistent Profile

Like a lot of administrators I work on a couple of different PC’s, and keeping my PowerShell profile the same on each machine is a bit of a pain.  I decided it was time to do something about that!  With these changes I can easily add to my profile and have that change happen on every PC I use!

What is a Profile?

If you’re new to PowerShell you may not even know what a Profile is, but essentially a profile is a script that automatically runs when you start PowerShell.  In that script you can pretty much do whatever you want, including putting in functions that will then be available to you whenever you want.  I won’t go into great detail on profiles here but would instead invite you to check out The Scripting Guy’s great explanation of it here.

The obvious problem here is the profile is specific to the computer, but if you use multiple computers you have to manually keep them updated and the same.  And frankly, I’m not too good at doing that kind of thing.  And like any good engineer I will gladly expend any amount of effort to avoid work!

Replication

The first hurdle is replication.  How do you get your files on multiple computers?  There are only about a thousand different ways of doing this, from Google Drive, to Sky Drive to who knows what.  The one I use is Dropbox.  It’s a got a very simple interface and replicates files from computer to computer effortlessly.  Some of the newer offerings out there offer more space for nothing so you should probably check them out but I’ve got everything on Dropbox already so that’s where I am.

First you need to create a folder on Dropbox where you’re going to store your functions and scripts.  Since my PowerShell scripts are located here:  c:\dropbox\scripts\ps scripts I decided to put my folder there.  I named it _ProfileFunctions (with the underscore) so it would be at the top of the folder list.  If I’m copying things into the folder I want it to be easy to find.  Since this is in my Dropbox folder it will automatically replicate to all of the computers that I have Dropbox installed on.

Loading the Profile

There are two pieces to this function.  One is a simple command that I have to manually place in my profile that calls another script.  The second script will loop through all of the PS1 files in _ProfileFunctions and load them.  That’s the dynamic part.  So first thing’s first is to modify my $Profile and add this line:

.”c:\dropbox\scripts\ps scripts\_ProfileFunctions\Function-Loader.ps1″

And here’s Function-Loader:

$Path = "c:\Dropbox\Scripts\PS Scripts\_ProfileFunctions"

ForEach ($Function in (Get-ChildItem $Path\*.ps1))
{  If ($Function.BaseName -ne "Function-Loader")
   {  .$Function.Fullname
   }
}

First edit the $Path line to match your own path.  Then the script simply loops through all *.ps1 files in your _ProfileFunctions folder, skipping itself.  Any PS1 files it finds it runs.  These scripts can pretty much do anything, but mostly I like to put functions in there.  I like to keep my functions in separate PS1 files in case I need to share them or pull them into other scripts.  This works out in that I can now drag those files into my _ProfileFunctions folder and they will automatically load when I start PowerShell.

This means, when I go to a new workstation and want to set it up, I only have to edit my $Profile and add that one line running Function-Loader.ps1 and when I open a new PowerShell window I get my normal setup!

A couple of my favorite settings?

Import-Module ActiveDirectory -ErrorAction SilentlyContinue
set-alias npp -value "C:\Program Files\Notepad++\notepad++.exe"
Set-Alias ssh -Value Enter-PSSession

I always have Remote Server Administration Tools (RSAT) installed on my workstation so the first line just loads them, and if I don’t have them loaded it just moves on without error.  Notepad++ is a great text editor I love using, but typing in Notepad++ every time I need it is a pain, so a quick alias to “npp” and I’m good.  I’m not a Linux guy, at all, but I really enjoyed the next alias which let’s me use PowerShell remoting to my servers by typing in SSH.  Now I can remotely control my servers just like the Linux people 😉   Gotta have fun with it too, people!

In comments, tell me what you like to load into your profile!

Advertisement

December 8, 2013 - Posted by | General |

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: