Search code examples
powershellsharepointsharepoint-2016

Attempting to remove Sharepoint Mysites with a script


I have about 900 old mysite sites that I need to delete. I attempted to script the process and it's not working, here is my script:

    <#Set Variables#>
$sites = Import-Csv C:\Scripts\termedusers.csv
$Date = Get-Date -Format 'MM/dd/yyyy'
<#Confirm Import#>
$sites.count

<#Remove Sites of employees who were termed over 3 years ago#>

ForEach ($site in $sites) {
    Get-SPSite -Identity $Site.SiteName

        If ($Site.TermDate -lt 12/31/2018)
        {
        Write-Host "Removing Site:" $Site.SiteName -ForegroundColor Green -Object | Remove-SPSite -Identity $site.SiteName -GradualDelete -Confirm:$False
        }
        Else 
        {
        Write-Host $Site.SiteName "Not Deleted"
        }
    }

But when I run the script I just get https://sitename.com/mysite/domain_user_name Not Deleted

I'm convinced that it has something to do with the date, termdate and less than, but since this is my first attempt at using dates, I'm not really sure where to go from here. Any guidance would be much appreciated!


Solution

  • Make sure you are comparing dates by adding Get-Date:

    If ((Get-Date $Site.TermDate) -lt (Get-Date '12/31/2018'))