Hotwheels
Hotwheels

Reputation: 25

Passing commands to cmd from powershell

I want to make changes to a registry key through this command:

REG ADD "HKLM\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQLServer\SuperSocketNetLib\Tcp\IPAll" /v TcpDynamicPorts /t REG_SZ /d 6363 /f

This has to happen in a cmd, which i ran as administrator through powershell with this command in a batch file:

powershell.exe Start-Process cmd.exe -Verb runAs

I need a UAC Prompt for the user to input his admin credentials to make it as user friendly as possible. Now my question: How do i pass the reg add command to the console which i started as administrator?

Upvotes: 0

Views: 1492

Answers (1)

Robert Dyjas
Robert Dyjas

Reputation: 5227

You need to pass your command in -ArgumentList parameter like this:

powershell.exe "Start-Process powershell -ArgumentList 'REG ADD "HKLM\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQLServer\SuperSocketNetLib\Tcp\IPAll" /v TcpDynamicPorts /t REG_SZ /d 6363 /f' -Verb RunAs"

This will execute PowerShell which tries to execute another PowerShell window asking you for credentials and then execute REG ADD command and close PowerShell at the end.

Keep in mind that you don't have error handling or anything like this so you may want to add them later as they might be very useful.

Upvotes: 2

Related Questions