You are looking for Invoke-Sqlcmd cmdlet.
Save your SQL Server command (below ones) in a file and save it as SQLInput.sql
create login [MYDOMAIN\myusername] from windows
go
sp_addsrvrolemember [MYDOMAIN\myusername], 'sysadmin'
go
Then, You can run PowerShell command prompt with Run As Administrator and run the below command like:
Invoke-Sqlcmd -InputFile "C:\SQLInput.sql" | Out-File -filePath "C:\SQLOutput.rpt"
EDIT:
You can use -Query option to run the query inline like
Invoke-Sqlcmd -Query "create login [MYDOMAIN\myusername] from windows;"
Catching response, is to make sure whether Query ran successfully or not. You can use -AbortOnError switch; which will abort the command on error.
Else, you can use Try .. Catch .. Finally construct in PowerShell like
Try
{
Invoke-Sqlcmd -Query "create login [MYDOMAIN\myusername] from windows;"
}
Catch
{
[system.exception]
"caught a system exception"
}
Finally
{
"end of script"
}
Read more about exception handling in Powershell Here