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;
        Write-host "Deleted $contentType content type from $ctuse.Url"
Write-host "Deleted $contentType from site."

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


Thanks for your help!



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.

  • This link is dead. – Nullldata May 8 '18 at 15:06

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

    [string]$sitename ,

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

