If you haven't yet started using PowerShell with SharePoint to manipulate administrative and developer tasks (especially when trying to debug things) you really should. It's a nice replacement for all of those things that in the past you might write a ton of little command line apps for. Additionally, if there are repetative tasks (such as with administration) that you do repeatedly, you can use this as a means for automating them easily with PS scripts.
Now, one of the irritating things I found as I started using PowerShell in this way is having to always "prepare" my PowerShell environment everytime I fired it up (loading the SharePoint assembly and getting my initial site object). Another peeve was that I keep forgetting to type new-object instead of simply new like I'm used to from C#. So I wrote a quick profile script to take care of these basics for me and I wanted to share that here so others can get moving more quickly.
The first thing you need to do is reset the execution policy from Restricted to RemoteSigned. Before doing this, type:
PS C:\users\kdostalek> get-help about_signing
And make sure you understand the security ramifications of making this change. If you are comfortable proceeding, type the following:
PS C:\users\kdostalek> Set-ExecutionPolicy RemoteSigned
Great, now we can add our own startup profile script. There are a bunch of places you can add this file...
a good reference can be found here, but if you want your scripts available to any user that logs on to the server you should put it here:
c:\windows\system32\windowspowershell\v1.0
Create a textfile in that directory named, profile.ps1, and open it up in notepad.
For this post, I just want to highlight 3 things to put in here that will make you life easier. The first is setting up an alias for the keyword new, and mapping it to the command new-object. The second is to automatically load the Microsoft.SharePoint assembly into the AppDomain so you can create objects from it. The third is a little helper function that takes an URL as a parameter and returns a SPSite object for you to start manipulating. Here is the script in total:
set-alias new new-object
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
function getsite {
$site = new Microsoft.SharePoint.SPSite($args)
return $site
}
Save the file, then fire up PowerShell. You should see it loading the SharePoint assembly from the GAC and you are ready to go. Now if you type in something like this:
PS C:\users\kdostalek> $s=getsite(http://intranet)
You will now have an instance of an SPSite object in the variable $s, all ready for you to start hacking away at.
There is obviously much much more you could add in the way of helper functions here for things you find yourself repetedly doing in your "interactive" sessions. And of course, anything you truly need to automate can be captured in a script. I'll try and post some of my favorite and most useful functions and scripts here in the future.