Jun 25, 2008

PowerShell & SharePoint - Quick Start

SharePoint and PowerShell are a perfect match. Here's the PowerShell profile I'm using on my Sharepoint Dev. Boxes.

If you haven't a PowerShell profile yet, create it:

  • >new-item -type file $profile -force
  • >notepad $profile
  • Paste in the following snippet or download:
$12HivesDir = "${env:CommonProgramFiles}\Microsoft Shared\web server extensions\12\"
 
#load Microsoft.SharePoint.dll
[System.Reflection.Assembly]::LoadFrom("$12HivesDir\ISAPI\Microsoft.SharePoint.dll")
 
#returns the SPSite at the specified URL
function get-spsite ([String]$webUrl=$(throw 'Parameter -webUrl is missing!'))
{
   return New-Object -TypeName "Microsoft.SharePoint.SPSite" -ArgumentList "$webUrl";
}
 
#returns the SPSite object from the specified URL
function get-spweb ([String]$webUrl=$(throw 'Parameter -webUrl is missing!'))
{
   $site =  New-Object -TypeName "Microsoft.SharePoint.SPSite" -ArgumentList "$webUrl";
   return $site.OpenWeb();
}
 
#returns the SPList object from the specified URL and List name
function get-splist ([String]$webUrl=$(throw 'Parameter -webUrl is missing!'),
[String]$listName=$(throw 'Parameter -listName is missing!'))
{
   $site =  New-Object -TypeName "Microsoft.SharePoint.SPSite" -ArgumentList "$webUrl";
   $web = $site.OpenWeb();
   return $web.Lists[$listName]
}
 
  • Save
  • >set-executionpolicy -executionPolicy remoteSigned
  • Reopen the PowerShell 

Now you have. Use it:

  • >$web = get-spweb http://localhost
  • >$web.Title = "Greetings from the Powershell"
  • >$web.Update()
  • Browse to http://localhost and see what's happen.

I've teamed up with Eric Kraus from Microsoft. You can find our joint project for PowerShell 2.0 called SPoshMod on CodePlex. Hope the first release will be availiable soon...

28 comments:

Anonymous said...

hi christian,

the script works flawless for me, cool!

anyways, you should consider to upload the script as .txt file, because if I c&p the script to my ps profile I run into troubles with linebreaks.

Christian said...

Thanks, you're right!
I've just updated the posting accordingly.
Now cou can download it.

Christian said...

Some ideas what you can do from Karine Bosch:
http://www.u2u.info/Blogs/Karine/Lists/Posts/Post.aspx?ID=9

Karine Bosch said...

Christian,
I just read your post. Thanks for suggesting the functions. Great post!
Karine Bosch

Anonymous said...

Christian,

the good part about this is, that it also works with WSS2.0.

because 12hive does not exist in old wss, you just need to adjust the path to the sharepoint dll.

[...]Microsoft Shared\web server extensions\60\ISAPI\Microsoft.SharePoint.dll

Ani said...

How Can I use this powershell script in dispform.aspx?

Actually I have infopath form library, then I customized search result, search result is working properly and displaying all the column, but in title it is linked with XML file name rather than dispform.aspx?id=???.

How can I get for each sharepoint inforpath form library line item?

Can you help me out?

Christian said...

Hi Ani,

you can't use the Script in an aspx-form. It only works in the PowerShell.

That the title column links the Form is by design.
What do you mean exactly with customized search result? You could affect the title column with a custom content type or customize the search result to show the column with the dispform.aspx?id=?.
Could you please explain the scenario more exactly.

Bye,
Christian

Jesper M. Christensen said...

Always nice to find great resource about Powershell to Sharepoint sites/lists.

Keep up the good work and thanks for the info.

Jurgis said...

Hi, post is very helphull.

How can I updaten a lits item.
after calling $item.Update()
I keep getting an error:

Exception calling "Update" with "0" argument(s): "You cannot call a method on a null-valued expression."

Thanks in andwance.

Christian said...

Hi Jurgis,
$item.Update() ist the right method. Seems you $item is null.
bye, Christian

Jurgis said...

Hi Christian,

Maby this will help.
I do the folowing steps:

$list = get-splist http://mysite Tasks
$item = $list.Items[5]
$item["Title"] = "Test1"
$item.Update()

And get the error.

Thanks in adwance.

Christian said...

Hi Jurgis,

code snippet looks good. Your exception seems caused through a null value in varibale $item, but then $item["Title"] = "Test1" should raise the error before update. Seems weird. Next week I'm in holiday so I can't help. Maybe my SPoshMod colleague Eric Kraus could help you http://blogs.msdn.com/ekraus/

Bye, Christian

Bye, Christian

Anonymous said...

For SP 2010, use
"${env:CommonProgramFiles}\Microsoft Shared\web server extensions\14\"
instead of
"${env:CommonProgramFiles}\Microsoft Shared\web server extensions\12\"

Cheap Chanel Handbags said...

I used to dislike overly sunny/ hot days because it gets a little too cheerful (haha), preferring days that are more melancholic, but wow, this rain is just downright depressing haha

Hug Day said...

happy hug day 2016 SMS
hug day 2016 Wishes cards greetings
Hug day Date 2016
hug day sms

Libin Huang said...

20160426libinhollister clothing store
oakley sunglasses
michael kors handbags
tiffany outlet
ray-ban sunglasses
gucci outlet online
ray-ban sunglasses
burberry outlet online
coach outlet canada
ray-ban sunglasses
herve leger outlet
ray ban sunglasses sale
air max 90
oakley sunglasses
adidas wings shoes
michael kors outlet
michael kors factory outlet
louis vuitton bags
michael kors online outlet
nba jerseys
ralph lauren pas cher
oakley sunglasses
toms shoes
nike air max 90
nike tn pas cher
air max 90
lululemon outlet
nike air force 1
true religion outlet
michael kors outlet
burberry outlet store
ray ban sunglasses
kobe shoes
true religion outlet
discount michael kors handbags

Yuanyuan Lin said...

7.14llllllyuan"ferragamo shoes"
"chrome hearts outlet"
"air jordan shoes for sale"
"tory burch outlet online"
"hollister"
"michael kors outlet"
"ralph lauren femme"
"thomas sabo uk"
"adidas uk store"
"louis vuitton outlet"
"coach outlet"
"coach handbags"
"burberry outlet"
"coach outlet online"
"rolex watches"
"cheap oakley sunglasses"
"tiffany outlet"
"cheap football shirts"
"celine outlet online"
"michael kors handbags clearance"
"snapbacks wholesale"
"michael kors outlet"
"toms outlet store"
"true religion outlet uk"
"coach outlet store"
"oakley sunglasses"
"celine outlet"
"swarovski outlet"
"cheap snapbacks"
"longchamp pas cher"
"michael kors outlet online"
"fitflops clearance"
"nike roshe run"
"coach outlet online"
"oakley sunglasses"
7.14

Justin said...

Purchasing protection is something that can make the expense go up a great deal. This is not something that you need to hold up to keep an eye on when the time has come to pay the last bill at the end of your rental. Payday Loans

Fangyaya said...

fitflop sandals
polo ralph lauren outlet
rolex submariner price
michael kors outlet
christian louboutin wedges
ralph lauren polo
louboutin shoes
abercrombie and fitch outlet
coach outlet canada
michael kors outlet
gucci outlet online
cheap louis vuitton handbags
ray bans
cheap oakley sunglasses
copy watches
adidas nmd
michael kors handbags
oakley canada
cheap rolex watches
ray ban sunglasses wholesale
hollister kids
true religion outlet
burberry sale
nfl jerseys
burberry scarf
cheap air jordans
replica watches
coach outlet
oakley sunglasses wholesale
oakley outlet
michael kors uk
ray ban sunglasses
adidas outlet store
rolex watches for sale
cheap oakley sunglasses
adidas originals store
rolex watches
cheap true religion jeans
ralph lauren outlet
kobe 11 shoes
20168.8chenjinyan

dada24 Xu said...

toms shoes outlet
ugg outlet
celine outlet store
red bottoms
moncler jackets
adidas nmd
longchamp bags
cheap jordans
ugg outlet
christian louboutin shoes
2016921caiyan

John said...

air force 1
birkenstock outlet
ugg sale
fitflops sale
michael kors outlet store
canada goose jackets
nike uk
converse all star
ralph lauren outlet online
coach outlet online
birkenstock outlet
abercrombie
polo ralph lauren outlet
adidas nmd r1
mcm outlet online
coach outlet online
timberland boots outlet
ed hardy store
true religion jeans outlet
nike cortez classic
timberland shoes
louis vuitton outlet online
birkenstock sale
louboutin outlet
michael kors handbags outlet
uggs sale
birkin handbags
rolex watches for sale
michael kors outlet store
vans outlet
polo ralph lauren outlet online
20161102yuanyuan

نور الهدى said...



افضل شركة نقل اثاث بالمدينة المنورة تساعدك على نقل اثاثك بامان فلا داعى للقلق مع افضل شركة نقل اثاث بجدة

عزيزى العميل انت من محبى التنقل باستمرار بالتالى انت بحاجة ماسة وضروية الى الاستعانة بالمختصين في نقل العفش خاصة ارخص شركة نقل اثاث بالرياض لان الاستعانة باى من عمالة الشوارع الغير مدربة والتي لا تمتلك خبرة كافية في نقل العفش او الحفاظ علية وليس هذا فقط فقد يؤدى الاستعانة بعمالة الشوارع الى حدوث حالة فقدان وتكسير للاثاث بالتالى التاثير الضار عليك عزيزى العميل

لا تقلق مطلقاً الان بشأن نقل اي منقولات خاصة بك طالما استعنت بشركة الاول لـ نقل الأثاث في الرياض وخارج الرياض فنحن ليس الوحيدون ولكننا متميزون عن اى مؤسسة أخرى داخل وخارج الدمام وشهرتنا كافضل شركة نقل عفش بينبع
نقل اثاث بجدة

ميدو جابر said...


هل انت ممن يريد ان يصبح بيته نظيفا طول الوقت هل تريد ازالة الاتربة والاوساخ من المنزل هل تريد معرفة طرق التنظيف الحديثة من قبل

شركة تنظيف منازل بالرياض شركة الاوائل التي تعد هي الافضل في الرياض والتي تستخدم افضل الطرق الحديثة والماكينات والالات
شفط الاتربة وكنسه جيدا
تلميع الابواب والحوائط والاسقف
تنظيف الدريش وتلميعه
غسيل الحمامات والمطابخ وغيرها
ازالة الاوساخ المتلصقة بالبلاط وتلميعه وتعقيمه جيدا
رش مبيدات بالمنزل لا ابادة الحشرات جيدا
في حالة وجود فرشات يتم تنظيف الفرشات جيدا وتجفيفة وتعقيمه
كل تلك الطرق تعطي نتائج كبرى وكبيرة وكل هذا لن تجده الا بشركة

شركة تنظيف بالرياض شركة الاوائل الشركة الاكثر طلبا بالرياض
وايضا تعد شركتنا الاكثر من بين الشركات في الاعتماد علي افضل العمالة المدربة والادوات الحديثة والسيارات المتخصصة لنقل العفش من والي من داخل الرياض الي خارج الرياض والممكلة با افضل النتائج وايضا اسعار معقوله تعد

شركة نقل اثاث بالرياض
شركة الاوائل الاكثر نجاحا في مدينة الرياض فك تركيب تغليف اتصل نصل اليك في الحال نتواجد في كافة الانحاء شرق شمال غرب جنوب الرياض ومعظم الاحياء كحي السويدى الملز النسيم الشفا الصحافة وادي لبن
ولا تتوقف خدمتنا عند تلك الحد في تلك المجالات ولكن انتشرت لتصل الي خدمات
كشف تسربات المياه
حيث احدث واكبر الاجهزة التكنولوجية الحديثة التي تكشف عن موقع التهريب دون الجو الي تكسير وايضا تقوم با اعمال الكشف عن المجارى وغيرها وتحدد سبب المشكلة وتقوم بمعالجته با اقل تكلفة بواسطة افضل الفنيون الماهرة

Meiqing Xu said...

oakley sunglasses
louis vuitton outlet
michael kors handbags
canada goose sale
true religion outlet
michael kors outlet clearance
ugg boots
pandora
michael kors handbags
jimmy choo
20161221caiyan

dong dong23 said...

nike air max thea
pandora bracelet
adidas nmd
ralph lauren outlet
gucci outlet online
ed hardy jeans
oakley sunglasses outlet
mbt outlet
michael kors bags
adidas outlet store
2017.5.11chenlixiang

xjd7410@gmail.com said...

polo ralph lauren outlet
michael kors handbags
mlb jerseys
cheap nfl jerseys
asics,asics israel,asics shoes,asics running shoes,asics israel,asics gel,asics running,asics gel nimbus,asics gel kayano
foamposite shoes
louis vuitton outlet
chrome hearts
oakley sunglasses sale
fitflops uk
chanyuan2017.05.24

Home Improvement City of Yonkers said...

The team at Ajrin Construction Inc. is the best of the best general contractors and support staffs, as selected from within the industries over City of Yonkers NY based upon their achievements and their understanding of complex projects.
Home Improvement City of Yonkers

sabahhassan said...

How Very much To Charge For Home Cleaning

Enjoy a clean floor covering with our professional floor covering washing program. Sometimes the problems involve misunderstandings, lack of clarification of what is to be cleaned and thoroughness of the job. I was told by one cleaning organization if I desired them to do windows, they'd just hire this same windowpane شركة صيانة مكيفات بالرياض cleaning organization and charge me a surcharge. I believe 25-35 dollars is normally not really poor depending on the quality of work and all that they do. While I can't afford $400.00 per month, I would recommend her becuase she murdered it and my house looked better than it did when I bought it 9 years ago.

They no longer use their personal products and right now they are using my G.T. I possess known lots of idiots with graduate student levels and lots of fair and informed human being beings who don't have اسعار تنظيف الخزانات eliminated to a day time of college. I have got discovered the cleaning companies, we possess used, start trimming edges after several appointments and need to become reminded what their agreement needs.

Yelp users never have asked any queries yet about Marin اسعار تنظيف المنازل بالرياض. No 1 knows what we possess to cope with until they put themselves in a housekeepers sneakers. As clean as others so they will take longer to clean and requires a 'DEEP CLEANING" which take a lot of elbow grease and product. We agreed on 4 hours, rather than a per-job fee. I have got received therefore many blessings by keeping a great attitude and stopping myself from resenting the "rich" people I proved helpful for.

Home cleaners who record income (yes, you can question for evidence), possess business expenditures. This prevents water buildup, which deters mold and maintains the sink looking clean much longer. I know the thought of cleaning for شركة تنظيف فلل بالرياض عمالة فلبينية free of charge is normally not really attractive but you will not really just gain very much required knowledge but you will obtain some good testimonials and references (which you will need when you obtain those 1st clients).

Yes she paid the transport, resort space, and most meals, but paid nothing for my time.I experienced actually taken time off from my regular job to go with her, and it was not a holiday for me, طريقة تنظيف المسبح بالمكنسه it was hard function. If you're worrying about the price of the services, then you should become cleaning your very own home. Go back and wipe down counters and rinse tub then clean mirrors.