11

I am developing in Sharepoint and I like more Windows Powershell than Sharepoint Management Shell. Every time I restart the development machine, I have to run Add-PSSnapin Microsoft.Sharepoint.Powershell in order to use Sharepoint cmd-lets.

How to get the machine to automatically load Sharepoint snapin when I start Windows Powershell.

  • create a file called profile.ps1 in C:Users\you\Documents\WindowsPowerShell. When you open powershell it will run profile.ps1 automatically. Type Add-PSSnapin .... to the profile.ps1. Note: You have to create the directory WindowsPowerShell in most cases – Kolob Canyon Oct 28 '16 at 18:47
12

In your PowerShell shell type

Notepad $PROFILE

This will give you the scripts that is triggered for the user that is logged in.

If your profile is not created, then you can create it with this command:

new-item $profile -type file -force

here you can insert scripts that are executed before startup.

Here you can add SharePoint snapin:

Add-PsSnapin Microsoft.SharePoint.PowerShell -EA 0

I usually here set stuff like

Set-Strictmode -Version Latest
$host.UI.RawUI.WindowTitle = "$env:UserName | $env:Computername"
[Environment]::CurrentDirectory = (Get-Location -PSProvider FileSystem).ProviderPath
  • 2
    rask: what is this wonderful -EA 0 parameter, undocumented ? – Steve B Jun 23 '11 at 12:22
  • 5
    -ErrorAction SilentlyContinue so it doesnt throw an error if module is loaded. Note that this will actually throw an error in SharePoint shell, since SharePoint doesnt check if module is already loaded :-( – Anders Rask Jun 23 '11 at 12:34
  • rask: it can be usefull in .ps1 file, as I can add the snapin in the head of the file, and run the ps1 either from a naked powershell console, or a sharepoint powershell console. – Steve B Jun 23 '11 at 12:41
  • When I run notepad $profile it says: The system cannot find the path specified. So I created this file manually and added Add-PsSnapin Microsoft.SharePoint.PowerShell -EA 0. Thank you for your answer! – Anatoly Mironov Jun 23 '11 at 14:08
  • yep, if its not created you just save it. Glad to be of help. – Anders Rask Jun 23 '11 at 14:12
1

If you're running Sharepoint 2010, use the SharePoint 2010 Management Shell located at:

Start | All Programs | Microsoft SharePoint 2010 Products | SharePoint 2010 Management Shell

This starts PowerShell and loads the SharePoint cmdlets. You'll probably want to Shift-Right Click this item and select "Run as administrator" from the context menu.

Note that this shortcut is only available on the SharePoint server or on a development workstation where SharePoint is installed locally.

  • Yes, you are right. But I like Windows Powershell GUI more. – Anatoly Mironov Jun 23 '11 at 14:32
  • Nah, screw GUI's – Kolob Canyon Oct 28 '16 at 18:51
0

If you create a shortcut with the following command line, you'll get the snapin installed:

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -NoExit add-pssnapin microsoft.sharepoint.powershell

(Obtained from TechNet)

  • It works. But is there is a possibility to add it to profile ps1 or something so one doesn't need to create custom shortcuts? I like the pinned shortcut on the panel. – Anatoly Mironov Jun 23 '11 at 11:20
0

Another related option is to set the value not just for the logged in user, but for all users on the device. This can be done with

notepad $PROFILE.AllUsersAllHosts

Any code you add in here will be run for all users. For example, adding this will automatically load the SharePoint snapin

If ((Get-PSSnapIn -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{ Add-PSSnapIn -Name Microsoft.SharePoint.PowerShell }
0

This solution is one PowerShell script that will create the AllUsersAllHosts file if necessary and then append the Add-PSSnapIn command and then restart PowerShell or ISE and is based on this and this.

if (!(test-path $profile.AllUsersAllHosts)){
    new-item -type file -path $profile.AllUsersAllHosts –force
}

$cmd = 'if((Get-PSSnapin | Where-Object {$_.Name -eq
"Microsoft.SharePoint.PowerShell"}) -eq $null) 
{ 
    Add-PSSnapIn "Microsoft.SharePoint.Powershell" 
}'

out-file -FilePath $profile.AllUsersAllHosts -InputObject $cmd -Append

# based on http://www.powershellmagazine.com/2014/03/04/pstip-restarting-powershell-as-an-administrator/
[CmdletBinding(SupportsShouldProcess,ConfirmImpact='High')]

$AsAdministrator=$false
$Force=$true

$proc = Get-Process -Id $PID
$cmdArgs = [Environment]::GetCommandLineArgs() | Select-Object -Skip 1

$params = @{ FilePath = $proc.Path }
if ($AsAdministrator) { $params.Verb = 'runas' }
if ($cmdArgs) { $params.ArgumentList = $cmdArgs }

if ($Force -or $PSCmdlet.ShouldProcess($proc.Name,"Restart the console"))
{
    if ($host.Name -eq 'Windows PowerShell ISE Host' -and $psISE.PowerShellTabs.Files.IsSaved -contains $false)
    {
        if ($Force -or $PSCmdlet.ShouldProcess('Unsaved work detected?','Unsaved work detected. Save changes?','Confirm'))
        {
            foreach ($IseTab in $psISE.PowerShellTabs)
            {
                $IseTab.Files | ForEach-Object {

                    if ($_.IsUntitled -and !$_.IsSaved)
                    {
                        $_.SaveAs($_.FullPath,[System.Text.Encoding]::UTF8)
                    }
                    elseif(!$_.IsSaved)
                    {
                        $_.Save()
                    }
                }
            }
        }
        else
        {
            foreach ($IseTab in $psISE.PowerShellTabs)
            {
                $unsavedFiles = $IseTab.Files | Where-Object IsSaved -eq $false
                $unsavedFiles | ForEach-Object {$IseTab.Files.Remove($_,$true)}
            }
        }
    }

    Start-Process @params
    $proc.CloseMainWindow()
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.