Use PnP PowerShell to add ContentType for your SharePoint site
You can achieve the task by using SharePoint GUI. However, if your sites collection has tens of hundreds sites and each site has more than one document library, it will become a nightmare for a SharePoint administrator to do the task by using GUI.
Luckily, there is PnP Powershell which can help us achieve the goal.
The steps will be like below:
#Step1: export your login credential to a secure file on your local machine
get-credential|export-clixml -path c:\safe\mycredential.txt
#Step2: import your credential to Powershell
$cred=import-clixml -path c:\safe\mycredential.txt
#Step3: connect PnP online
connect-pnponline -url “your site url here” -credentials $cred
#Step4: get all sub sites of your site collection
$subsites=get-pnpsubwebs -recurse|select-url
#Step5: Use for each loop to loop through each subsites and add the content type into document libraries in each sub site.
foreach ($site in $subsites) {
connect-pnponline -url $site.url -credentials $cred
$docids=get-pnplist|where-object {$_.basetemplate -eq 101 -and $_title -ine “Site Assets”}|select id
foreach ($docid in $docids) {
add-pnpcontenttypetolist -list $docid.id -contenttype "content type name of your choice for default one" -DefaultContentType
add-pnpcontenttypetolist -list $docid.id -contenttype "2nd content type"
}
}
To remove the contenttype from your sharepoint libraries, you need to use remove-pnpcontenttypefromlist command.
See the following code:
Foreach($site in $subsites) {
connect-pnponline -url $sites.url -credentials $cred
$docids=get-pnplist|where-object {$.basetemplate -eq 101 -and $.title -ine “Site Assets”}|select id
foreach ($docid in $docids) {
remove-pnpcontenttypefromlist -list $docid.id -contenttype “the name of the content type you want to remove”}
}
}
Related Articles
PowerShell Escape Room
PowerShell Escape Room by Michiel Hamers by Michiel Hamers https://about.me/michielhamers/ Why on earth you want to create an Escape Room with PowerShell as backend? I’ve always been a fan of escape rooms, so I decided to create my own for my kids. I wanted to make it something that would be challenging and fun for them, but also educational. I decided to use PowerShell as the backend for the escape room, as I’m a PowerShell developer and I thought it would be a great way to learn more about the language.
Microsoft Graph PowerShell Module: Getting Started Guide
Microsoft Graph PowerShell Module: Getting Started Guide by Jeff Brown Microsoft is retiring the Azure AD Graph API sometime after June 30, 2023 (announcement). This retirement includes the Azure AD PowerShell module. In its place, Microsoft has released the Microsoft Graph PowerShell module. The Microsoft Graph PowerShell module is the next-generation way of managing Microsoft cloud services using PowerShell. If you have used MSOnline or Azure AD PowerShell in the past, you’ll need to read on to learn about this new module.
ICYMI: PowerShell Week of 08-October-2021
Topics include VMWare, Windows 11, Web Reports and more… Special thanks to Robin Dadswell, Prasoon Karunan V, Kiran Patnayakuni and Kevin Laux How to gather your vCenter inventory data with this VMware PowerShell script by Scott Matteson on 7th October Inventory reports are a common request when administering a VMware vCenter environment. Learn how this VMware PowerShell script can make such requests quick and easy Building a Web Report in PowerShell, use the -Force Luke by Chris Noring on 8th October