With SharePoint 2010, the usage of the Management Shell is pretty much mandatory. However, most corporations have a policy that says something along the lines of “Do not log in with Service Accounts” with the “or else” being implied. Unfortunately, the 2010 Management Shell usually only works properly when logged in as one of the system accounts (usually the setup or farm/admin account). This means that you are left with either logging into the server as the Service Account or using the shift-right-click trick on the Management Shell link and entering the farm account information there – every time.
If your company is anything like mine, the passwords for those service accounts are often an utterly bizarre and random combination of letters, numbers, symbols and stargate coordinates. This also means that you have to go digging for that password every time that you need to use the management shell because remembering such a twisted concoction is not really an option.
There is another way that I’ve found that is better than both of the above options. Simply create a batch file (really!) and enter the commands below and then save it somewhere where you can remember it – or, better yet, create a shortcut to it on your desktop.
|set PSshell="C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe -NoExit & 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG\POWERSHELL\Registration\\sharepoint.ps1'\""|
|runas /user:myDomain\ServiceAccountName /savecred %PSshell%|
|if errorlevel 1 pause|
This will start the management shell but will run it as the service account (be sure to replace ‘myDomain\ServiceAccountName’ with your service account info). Best of all, the /savecred option makes it so that you only have to enter the password the very first time you run the batch file which means no more digging around for it. (Note: the credentials are encrypted and tied to your ID on that machine so if someone else logs on and clicks the same batch file, they will be prompted for the password)
Simple and quick, and it still lets me log into the SharePoint server with my normal admin account.
Update 12/4/2012: Corrected a typo and moved the code to Gist on GitHub.
One thought on “Running the SharePoint 2010 Management Shell as a different user”
Wow, great script! This really helped me out.