# Step 1 - Basics, using the -query parameter
PS SQLSERVER:\SQL\XU-ACER-PC\DEFAULT\Databases> dir | %{$DBName=$_.Name;invoke-sqlcmd -database
$DBName -query
"create schema NoMoreForEachDB";$DBName}
PS SQLSERVER:\SQL\XU-ACER-PC\DEFAULT\Databases> dir | %{$DBName=$_.Name;invoke-sqlcmd -database
$DBName -query
"drop schema NoMoreForEachDB";$DBName}
# Step 2- Use an input file, using the inputfile parameter
(put the normalT-SQL codes into a file)
PS SQLSERVER:\SQL\XU-ACER-PC\DEFAULT\Databases> dir | %{$DBName=$_.Name;invoke-sqlcmd -database
$DBName -InputFile
"C:\users\Xu-Acer\documents\createtable.sql";$DBName}
# step 3 - more easily extensible than sp_MSForEachDB based on
your filtering requirements