PowerShell Script to Find Lists with Null Default View in SharePoint 2010

  1. $LogTime = Get-Date -Format yyyy-MM-dd_hh-mm  
  2. $LogFile = ".\FindSPNullDefaultViewPatch-$LogTime.rtf"  
  3.  
  4. # Add SharePoint PowerShell Snapin  
  5.   
  6.   
  7. if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) {  
  8.     Add-PSSnapin Microsoft.SharePoint.Powershell  
  9. }  
  10.   
  11. $scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent  
  12. Set-Location $scriptBase  
  13.  
  14.  
  15. #Deleting any .rtf files in the scriptbase location  
  16. $FindRTFFile = Get-ChildItem $scriptBase\*.* -include *.rtf  
  17. if($FindRTFFile)  
  18. {  
  19.  foreach($file in $FindRTFFile)  
  20.   {  
  21.    remove-item $file  
  22.   }  
  23. }  
  24.   
  25.   
  26. start-transcript $logfile  
  27.   
  28. function FindSPNullDefaultView{  
  29.   
  30.     [cmdletbinding()]      
  31.     param(  
  32.         [parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True,Mandatory=$True)]  
  33.         [string[]]$url  
  34.     )  
  35.     Begin{  
  36.         $const_verbosepreference = $verbosepreference  
  37.         $verbosepreference = "Continue"  
  38.          
  39.         Try { [void] [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") }  
  40.         Catch {  
  41.             Write-Warning "Failed to Load REQUIRED .NET Framework Assembly Class...exiting script!"  
  42.             Break  
  43.         }  
  44.         $array = @()  
  45.     }  
  46.     Process{  
  47.         Try{  
  48.             $SiteCollection = New-Object Microsoft.SharePoint.SPSite($url)  
  49.             Foreach($Site in $SiteCollection.AllWebs){  
  50.                 Write-Verbose "$($Site.Url)"  
  51.                 Foreach ($List in $Site.Lists){  
  52.                     Try{  
  53.                         If($List.DefaultView -eq $null){  
  54.                             $props = @{  
  55.                                 Title=$($List.Title)  
  56.                                 ItemCount=$($List.ItemCount)  
  57.                                 Author=$($List.Author)  
  58.                                 LastItemModifiedDate=$($List.LastItemModifiedDate)  
  59.                                 ParentWebUrl=$($List.ParentWebUrl)  
  60.                                 BaseType=$($List.BaseType)  
  61.                                 RootFolder=$($List.RootFolder)  
  62.                                 Hidden=$($List.Hidden)  
  63.                             }  
  64.                             $array += New-Object PSObject -property $props  
  65.                         }  
  66.                     } Catch { Write-Verbose "Unable to load $List.DefaultViewUrl" }  
  67.                 }  
  68.             }  
  69.             $SiteCollection.Dispose();  
  70.         } Catch { Write-Verbose "Unable to locate site for $($url)" }  
  71.     }  
  72.     End{  
  73.         $array  
  74.         $verbosepreference = $const_verbosepreference  
  75.     }  
  76. }  
  77.   
  78. $siteCollectionURL = read-host "Enter the site collection url "  
  79. FindSPNullDefaultView $siteCollectionURL  
  80.   
  81. stop-transcript