Friday, July 25, 2014

Find the directory path where a running script, PowerShell 3.0 style

Back in the good old days of PowerShell 2.0, we often started our scripts with a line that referred the directory path where the script ran.

This is used e.g. to get location of a WSP package that was to be deployed to the farm. The PowerShell script looked like this.

1 $scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
2 $scriptPath + "\mypackage.wsp"
3 #add and deploy package to farm

But in PowerShell 3.0, there is a new build-in variable for this. The “$PSScriptRoot” and now the PowerShell look like this


1 $PSScriptRoot + "\mypackage.wsp"
2 #add and deploy package to farm

This a not a big change to the script, but a line less is a win.

Thursday, June 26, 2014

Simplify your PowerShell and remember | is your friend

I had a colleague last week who wanted to clean up in his web application for site collection that he did not use. He google and found this piece PoweShell.

1 $tmpRoot = Get-SpWebApplication -Identity http://w520-sp2013-1:81
2 $tmpRootColl=$tmpRoot.Sites
3 for ($index=$tmpRootColl.Count-1 ; $index-ge 0 ; $index--) {Remove-SPSite -Identity $tmpRootColl.Item($index) -GradualDelete -Confirm:$false}
It now works fine too, but he had a hard time reading it and it could easily be made more simple. Eg. this way.


1 Get-SpWebApplication -Identity http://w520-sp2013-1:81 | Get-SPSite -Limit:All | Remove-SPSite -GradualDelete -Confirm:$false
Now there's no loop or variable you must remember to instantiate.

Wednesday, June 11, 2014

“Run as Administrator” the easy way

We live in a world that is run via PowerShell and how often have you not forgot to run his PowerShell script as "run as administrator"? But there are a few tricks in Windows so that it become easy.

As you probably already, how to start a problem with administrative privileges by right-clicking on a program/shortcut and select "Run as Administrator".
clip_image001

But did you know that by holding ctrl+shift down, you can obtain the same?

You can either double-click on a program/​​shortcut while holding ctrl+shift down or hit enter, it is the same.

Another trick is to make a shortcut to your program. Because on a shortcut, you can choose whether it always should "run as administrator"

How do you know about a problem running under the administrator context?

Look up at the program title bar. It says "Administrator" if it is running under sub-administrator context. This works not only for PowerShell command prompt but also for e.g. visual studio.

Untitled3

Thursday, May 29, 2014

Set the column width i SharePoint

Just a small script for safe keeping. This script will search for column with a title e.g. Name and set the column width. To set more columns, add new line to the script and change the selector

1 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js" type="text/javascript">
2 </script><script type="text/javascript">
3 $(function(){
4 $(".ms-viewheadertr th:contains('Name')").css("width", "50px");
5 });
6 </script>
7

This script is tested with document libraries both on 2010 and 2013.

Tuesday, May 27, 2014

SPBG user group meeting in Aarhus August 15.

There are arrange knowledge sharing meeting about Self-service BI capabilities of Office 365 and SharePoint dinner on Aug. 15 in Aarhus. Read more about the event here http://spbg.dk/Lists/Mder/DispForm.aspx?ID=70