Problem:
I got this error message in PowerShell ISE 4.0 when I run the script below. But When I run the same command in PowerShell 4.0 and Start PowerShell from SQL Server, it works fine.
PS SQLSERVER:\sql\localhost\default\databases> dir | %{$DBName=$_.Name;invoke-sqlcmd -database $DBName -query "drop schema NoMoreForEachDB";$DBName}
Solution:
Run PowerShell ISE 4 as Administrator