I am using the following power shell script:


To change the content type from the standard "document" content type to a new one in a document library, This works fine but i have a requirement that i only need to apply a certain document type to documents and documents in sub folders which are only in a certain root folder in the document library.

This has stump me on how you tell in to only look in a certain folder, I belive is something along this line: I need to tell the powershell script to select the folder like this, but what ever i try does not seem to work.


but then i thought it might not work like that i might have to do some looping of folder names like in the follow link to select the correct folder:

Get all document from all folder and subfolder from document library sharepoint powershell

2 Answers 2


I Found out that each item hold the full url under this value:


So I used the following IF statement to match the start of the file url path to match what folder i want to apply the content type to:

if ($_.File -like "$nameofdoclibrary/$Folder_root/*"){

$_["ContentTypeId"] = $newCTID



I found that the following line of code works for me In Power Shell after a ton of work $F.Properties["ContentTypeId"]=$ProjectFolder.Id.ToString() $F is the folder from $Web.GetFolder. $ProjectFolder is the new ContentType

Full code below. Add-PSSnapin Microsoft.SharePoint.PowerShell -EA SilentlyContinue Start-SPAssignment –Global $Site = "http://myreallykewlsite" $File = "TheFolderToUpdateinSite" $Title="NewTitlebyRodney"


$web = Get-SPWeb $Site $FolderType = $web.AvailableContentTypes["Folder"] $ProjectFolder = new-object Microsoft.SharePoint.SPContentType($FolderType, $web.ContentTypes, $ContentType) $FolderType.Group = "Folder Content Types" $web.ContentTypes.Add($ProjectFolder) $web.Fields.Add("ProjectInfo","Text",$false) $ProjectInfo =$web.Fields.GetField("ProjectInfo") $ProjectInfo.Group="Folder Content Types"

step 9

$FieldLink = New-OBject Microsoft.SharePoint.SPFieldLink($ProjectInfo) $ProjectFolder.FieldLinks.Add($FieldLink) $ProjectFolder.Update() Stop-SPAssignment –Global

Get the documents Library

$List = $web.Lists["Documents"]

update the library to allow managment of content types then addd the contenttype to the library.

$List.ContentTypesEnabled =$True $List.Update()

$List.ContentTypes.Add($ProjectFolder) $List.Update()

add the field to the Default View.

$D=$List.DefaultView $D.ViewFields.Add($ProjectInfo) $D.Update()

Get the List Item by its Title field value

$ListItem = $List.Items | Where {$_["Name"] -eq $File}

#foreach($_ in $ListItem){

Set "Department" column value

ok now to change the type from "Folder" to "ProjectFolder"

and add the Project Title and Info information

$F=$Web.GetFolder($Site + "/Shared Documents/" + $File) #fojoadsfjadslfj here is your error Rodney #$F.Item.File.CheckOut() #$FI.CheckOut() $F.Properties["ContentTypeId"]=$ProjectFolder.Id.ToString() #$FI.CheckIn() $F.Update() $F["Project"] = $Title $F.Update()


"Updated: " + $Site + " : " + $File + " : " + $Title

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.