Powershell script executed by SQL Server doesn't take effect I'm doing some testing and I want to execute a PowerShell script through xp_cmdshell. Below is the PowerShell script (c:\temp\sqltotext.ps1).
echo &quot;haha&quot; | Out-File &quot;c:\temp\haha.txt&quot;

Then I execute the script through xp_cmdshell like this.
DECLARE @result INT;
EXEC @result = sys.xp_cmdshell N'powershell.exe -Command &quot;Start-Process powershell.exe -Verb RunAs ''-NoProfile -ExecutionPolicy Bypass -File c:\temp\sqltotext.ps1''&quot;';
IF (@result = 0) PRINT 'Success';
ELSE PRINT 'Failure';

It runs successfully and I can find the file c:\temp\haha.txt with the content haha.
However, when I change the content of c:\temp\sqltotext.ps1 to:
echo Process CurrentUser LocalMachine | %{Set-ExecutionPolicy bypass -Force -Scope $_}

and execute the same aforementioned TSQL command, the TSQL reports success but I didn't get the expected result (the execution policy in all scopes were NOT changed).
When I execute the PowerShell script manually (in a PowerShell console and type c:\temp&gt;.\sqltotext.ps1), it works as expected (the execution policy in all scopes were changed). Why this happens?
I did some investigation though. EXEC xp_cmdshell 'whoami.exe' reports I'm running with nt service\mssqlserver. I also manually added nt service\mssqlserver into the administrators local user group. By using process explorer, I can confirm the PowerShell session indeed was started and all related processes have admin permission (Integrity = High).

The cmd.exe command line looks like this:

When I execute this command manually, it can change the execution policy without any issue (I changed all execution policy back to the original values after this).

The PowerShell process command line looks like this:

When I execute the PowerShell process command line manually, I can change the execution policies too. I don't have any idea why when I run the command through xp_cmdshell, it doesn't change anything.
BTW, I know there are multiple ways to write the command. I'm just talking about the technical skills here, so please don't suggest me changing command syntax etc.
xp_cmdshell is not PowerShell.
If you run: xp_cmdshell 'whoami.exe'; you will discover that you are nt service\mssqlserver and not Administrator.

xp_cmdshell is impersonating that user (which is a service in our case).
So the solution of the problem should be to impersonate an Administrator.
But you cannot do that.
I have tried using Proxy Credentials:
EXEC sp_xp_cmdshell_proxy_account 'ADVWKS\Max04', 'ds35efg##65';  
GO 

But I couldn't impersonate Administrator.
I have also tried adding this code at the beginning of the script:
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] &quot;Administrator&quot;)) { Start-Process powershell.exe &quot;-NoProfile -ExecutionPolicy Bypass -File `&quot;$PSCommandPath`&quot;&quot; -Verb RunAs; exit }

But the code just run forever because is expecting you to click on the YES and impersonate the Administrator.
I have also tried to run it from an Agent job: no joy.
This guide says not to impersonate a domain Admin. Which is exactly what you want to do. I let you try that because I cannot do that on my PC.
For start debugging try to run this:
runas /user:domain\administrator $^

and check if you can at least be Administrator on that machine.
So the recipe is:

Try to impersonate Administrator and execute the .ps1 script from command line
In alternative add a user with Administrator privileges
Impersonate it through SSMS

Good luck.