SharePoint24x7 It's all about SharePoint.


Tips n Tricks – tweaking the Windows PowerShell Profile

Windows PowerShell is the primary scripting tool available for SharePoint Administrators and Developers. Scripting was something most of the developers wanted to stay away from. But now it’s time for all the developers to master Windows PowerShell scripting since it is the core scripting platform from Microsoft. Just like Shell Scripts in Unix and Linux platforms, Windows PowerShell is the key to scripting within Microsoft technology stack.

Let’s get started with Windows PowerShell. Smile

When it comes to writing PowerShell scripts for SharePoint, we have no of tool options:

  • Windows PowerShell console window
  • Windows PowerShell Integrated Scripting Environment (ISE)
  • SharePoint 2013 Management Shell

Most of us who are into SharePoint, always prefer using SharePoint 2013 Management Shell since it loads all the SharePoint cmdlets by loading Microsoft.SharePoint.PowerShell.dll into the scripting environment.

If we are going to use Windows PowerShell console window or Windows PowerShell ISE, we will have to explicitly load the Microsoft.SharePoint.PowerShell.dll by executing following PowerShell command in order to being able to use SharePoint cmdlets.

Add-PSSnapin Microsoft.SharePoint.PowerShell

If you are writing very large, interactive scripts very often, it is probably easier to use the Windows PowerShell ISE, rather than the PowerShell console window or the SharePoint 2013 Management Shell because this provides a better editing and debugging environment. However, downside of using the PowerShell ISE is you will have to load the Microsoft.SharePoint.
PowerShell snap-in into the ISE as well every time you open it. It will be done by using exactly the same script shown above.

If you find yourself using the PowerShell ISE a lot, you can be smart to save your effort and time by adding the SharePoint snap-in automatically when the PowerShell ISE starts. You can do this by tweaking the Windows PowerShell profile.

The Windows PowerShell profile is a Windows PowerShell script file that runs every time you start Windows PowerShell. It has a .ps1 extension like any other normal Windows PowerShell script file. We can put any valid Windows PowerShell cmdlet in it so that they get executed every time we start Windows PowerShell. This is very similar to using Environment variables concept to configure startup options back in Windows Command Prompt tool. The only way in which the PowerShell profile file differs from a normal PowerShell script file is in its file name and file location.

If you are planning to use the PowerShell profile to configure startup cmdlets, as the first step, we need to figure out whether a PowerShell profile already exists on the server. We can figure out whether a PowerShell profile already exists by issuing following command:

Test-Path $profile

Check for profile

Check for profile

If the PowerShell profile already exists, above command will return “True”. If it doesn’t exist, it will return

If it returns either True or False, (i.e: whether the PowerShell profile exists or not), if you want to find out the default location of the PowerShell profile, you can run the following command:


Location of the profile

Location of the profile

And then, we can use Windows Explorer to browse to the path returned to make required modifications into the PowerShell profile script file.

If there is no file in that location. it means PowerShell profile does not exists and we need to create a new profile.

Use following command to create a new PowerShell profile if one doesn’t already exist.

New-Item -path $profile -type file

In case, if you receive an error response similar to one shown below as a result of executing the above command, make sure you have the correct folder path already in placed for the command to go and create the script file.

Error creating the profile

Error creating the profile

If the folder path exists and if it does create the PowerShell profile file successfully, you should receive the following response.

Creating the profile

Creating the profile

Use Windows Explorer to navigate to the file location and open the profile script file using any text editor such as Notepad.

Locating the profile

Locating the profile

Enter the following PowerShell command in the file, Save the script file and close the text editor.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction "SilentlyContinue"

Tweaking the profile

Tweaking the profile

Now you are all set to use Windows PowerShell ISE and Windows PowerShell console window to write SharePoint scripts. Now when you open either Windows PowerShell ISE or Windows PowerShell console window, it reads the PowerShell profile script file and load SharePoint PowerShell snap-in automatically.

Happy scripting…!!! Smile

Posted by Joy