If you want Set-ExecutionPolicy to work without prompting, add the -Force switch as well. (However, as has been noted, you can't rely on doing this from inside a script, since the execution policy would already have to be set up to allow scripts to run before that line was executed anyway.)
piping (echo "Y") to a command is an old Command Prompt trick which sometimes worked, but is never appropriate when working with PowerShell cmdlets or functions.