Powershell function to drop table(s) from SQL Server database ### List of tables that require full sanitizing
$requiredTables = 'table1','table2','table3'

foreach ($table in $requiredTables){

function Delete-FromTable{
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString =  "Server = $SQLServer; Database = $SQLDBName; Integrated Security = True;"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = 

"
delete from $table
"

$SqlCmd.Connection = $SqlConnection
$SqlCmd.CommandTimeout = 0
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$test=$SqlAdapter.Fill($DataSet)
$SqlConnection.Close()
$DataSet.Tables[0]
}

Delete-FromTable

}
Here's how I'd go about this:

function Get-IfSqlTableExists {
    param (
        [Parameter(Mandatory = $true)]
        [string]$SqlServerName,

        [Parameter(Mandatory = $true)]
        [string]$DatabaseName,

        [Parameter(Mandatory = $true)]
        [string]$TableName
    )

    $ConnectionString = "data source = $SqlServerName; initial catalog = $DatabaseName; trusted_connection = true;"
    $SqlConnection = New-Object System.Data.SqlClient.SqlConnection($ConnectionString)

    $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
    $SqlCmd.Connection = $SqlConnection
    $SqlCmd.CommandText = "select count(*) from sys.tables where object_id = object_id(@table_name);"

    $TableNameParam = New-Object System.Data.SqlClient.SqlParameter("@table_name", [System.Data.SqlDbType]::NVarChar, 512)
    $TableNameParam.Value = $TableName
    $SqlCmd.Parameters.Add($TableNameParam) | Out-Null

    try {
        $SqlConnection.Open()
        $Return = $SqlCmd.ExecuteScalar()
    }
    finally {
        $SqlCmd.Dispose()
        $SqlConnection.Dispose()
    }

    if ($Return -eq 1) {
        return $true
    }
    else {
        return $false
    }
}

function Delete-SqlTable {
    param (
        [Parameter(Mandatory = $true)]
        [string]$SqlServerName,

        [Parameter(Mandatory = $true)]
        [string]$DatabaseName,

        [Parameter(Mandatory = $true)]
        [string]$TableName
    )

    if (!(Get-IfSqlTableExists -SqlServerName $SqlServerName -DatabaseName $DatabaseName -TableName $TableName)) {
        Write-Warning "$TableName doesn't exist in $DatabaseName on $SqlServerName"
        return
    }

    $ConnectionString = "data source = $SqlServerName; initial catalog = $DatabaseName; trusted_connection = true;"
    $SqlConnection = New-Object System.Data.SqlClient.SqlConnection($ConnectionString)

    $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
    $SqlCmd.Connection = $SqlConnection

    # commented out the actual drop table command text to 
    # prevent accidental running
    #$SqlCmd.CommandText = "drop table $TableName;"

    try {
        $SqlConnection.Open()
        $SqlCmd.ExecuteNonQuery() | Out-Null
    }
    finally {
        $SqlCmd.Dispose()
        $SqlConnection.Dispose()
    }

    Write-Verbose "Successfully deleted $TableName in $DatabaseName on $SqlServerName"
}

$SqlServerName = "localhost\sql2012"
$DatabaseName = "TestDatabase"
$TableNameList = "table1", "table2", "table3", "notarealtable", "table4"

foreach ($TableName in $TableNameList) {
    try {
        Delete-SqlTable -SqlServerName $SqlServerName -DatabaseName $DatabaseName -TableName $TableName -Verbose
    }
    catch {
        Write-Error "Error attempting to drop table $TableName in $DatabaseName on $SqlServerName"
        # or handle however you want
    }
}


It looks like a lot of code, but it would scale.  It does things like checking to see if the table actually exists before attempting to delete.

My output on my test instance (with verbose output) looks like this:

VERBOSE: Successfully deleted table1 in TestDatabase on localhost\sql2012
VERBOSE: Successfully deleted table2 in TestDatabase on localhost\sql2012
VERBOSE: Successfully deleted table3 in TestDatabase on localhost\sql2012
WARNING: notarealtable doesn't exist in TestDatabase on localhost\sql2012
VERBOSE: Successfully deleted table4 in TestDatabase on localhost\sql2012


You could (and should) form this type of logic in any way you want.  This is just one approach with a style I typically like to take.

Note: I commented out the actual DROP TABLE command from the function to prevent accidental running.