0
votes

I have a .ps1 script that I need to launch in Powershell v2.0. Because of this, I use a shortcut file to launch it in Sta mode:

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -Sta -File "C:\TestFile.ps1"

This launches Powershell in Sta mode and the file is able to run. However, this PowerShell script uses New-ADUser and runs into Access Denied issues when trying to add the user. From what I can tell, this is due to needing to run PS in Administrator mode. However, I'm having trouble launching it in both Admin and -Sta mode at runtime, and I'm not really sure what to do.

Is there a way I can use the shortcut to launch Powershell as administrator AND -Sta?

2
New-ADUser needs to run using the credentials of a user with permissions to add users in AD. Running it as (local) Administrator should not help you there..Theo

2 Answers

0
votes

Right click the shortcut and select Properties from the context menu. When the properties window comes up select the Shortcut tab if it is not already selected, and click the Advanced button. When the new window comes up check the checkbox for Run As Administrator. Click OK, and then OK again. Now when you run that shortcut it will run as admin each time.

0
votes

You can try launching runas first:

C:\Windows\System32\runas.exe /netonly /user:YourDomain\YourAdminUserAccount %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -Sta -File "C:\TestFile.ps1"