0

I am new to PowerShell script. I am trying to run this specific script using .ps1 but received an error. Where do I put our SharePoint URL and the Content Type name in the script. Here is the script below:

$sitename = $args[0]
$contentType = $args[1]

$web = Get-SPWeb $sitename
$ct = $web.ContentTypes[$contentType]

if ($ct) {
$ctusage = [Microsoft.SharePoint.SPContentTypeUsage]::GetUsages($ct)
  foreach ($ctuse in $ctusage) {
        $list = $web.GetList($ctuse.Url)
        $contentTypeCollection = $list.ContentTypes;
        $contentTypeCollection.Delete($contentTypeCollection[$ContentType].Id);
        Write-host "Deleted $contentType content type from $ctuse.Url"
        }
$ct.Delete()
Write-host "Deleted $contentType from site."

} else { Write-host "Nothing to delete." }

$web.Dispose()

Thanks for your help!

Matthew

2 Answers 2

1

You need to pass the values in as arguments to the script.

The first argument is the site URL, the second is the content type name.

See http://www.powershell.nu/2009/12/16/running-scripts-with-arguments-in-powershell/ for examples of how to do this, depending on how you invoke the script.

1
  • This link is dead.
    – user43832
    Commented May 8, 2018 at 15:06
0

You have to add the following snippet at the top of your code:

Param(
    [Parameter(Mandatory=$True, Position=1)]
    [string]$sitename ,
    [Parameter(Mandatory=$True, Position=2)]
    [string]$contentType
)

Be careful, nothing should be addbefore this, except comments. The snippet allows the script to get input parameters entered by the user. You can also use it like this:

your_powershell_script.ps1 <http://your_site_name> <your_content_type_name>

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.