6

I'm learning Powershell, I have this line at the top of example script, but it doesnt appear to be necessary. What does it do? Why would it be there?

# SharePoint DLL
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

For example this example script to get content types works if the above line is commented out.

# SharePoint DLL    
# COMMENTED [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")


#Cmdlet Install
 Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue


# Get content types in a site collection.
# ----------------------------------------
function GetContentTypesInSite()
{
    $site = new-object Microsoft.SharePoint.SPSite($SiteURL)

    $web = $site.rootweb.ContentTypes
    ForEach ($id in $web)
    {
         '"' + $id.Name + $id.Id + '"'  | Out-File $OutputFile -append
    } 
}   


$SiteURL = "http://server:33333"
$OutputFile = "C:\temp\contenttypes.txt"
GetContentTypesInSite

I'm using the Windows PowerShell ISE to run the scripts

1 Answer 1

10

This used to be the way to load the sharepoint c# library in sharepoint 2007

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

In sharepoint 2010 this command does the same thing and more (adds 2010 commandlets)

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

There is usually no harm in running both, and it gives you consistent includes across versions

And if you use the sharepoint powershell command line, it will do the includes for you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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