Share via


start powershell script from cmd as admin

Question

Friday, October 11, 2013 10:44 AM

Hi,

I need to start a powershell script from a CMD file. The CMD window is started by another process and is not elevated, but the powershell script must start as elevated because of a dependency to a module which requires it.

The cmd file looks like:

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -Verb Runas -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -File .\Install.ps1

I'm getting an error:

-Verb : The term '-Verb' is not recognized as the name of a cmdlet, function, script file, or operable program.

Any ideas?

All replies (5)

Friday, October 11, 2013 12:06 PM ✅Answered | 1 vote

Verb is a option of the Start-Process cmdlet.

try this:

powershell.exe "Start-Process powershell -ArgumentList '-ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -File c:\install.ps1' -Verb RunAs"

Friday, October 11, 2013 11:07 AM

Hi,

what is "-Verb"??

try this:

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -File .\Install.ps1

Friday, October 11, 2013 11:24 AM

Hi,

I believe it's a property of the StartInfo object. What you wrote does not elevate the script.


Friday, October 11, 2013 11:33 AM

You think wrong

-Verb is not a option/switch from powershell.exe

You wrote:

"-Verb : The term '-Verb' is not recognized as the name of a cmdlet, function, script file, or operable program."

and that is right!

powershell.exe /?PowerShell[.exe] [-PSConsoleFile <file> | -Version <version>]    [-NoLogo] [-NoExit] [-Sta] [-NoProfile] [-NonInteractive]    [-InputFormat {Text | XML}] [-OutputFormat {Text | XML}]     [-WindowStyle <style>] [-EncodedCommand <Base64EncodedCommand>]     [-File <filePath> <args>] [-ExecutionPolicy <ExecutionPolicy>]    [-Command { - | <script-block> [-args <arg-array>]                  | <string> [<CommandParameters>] } ]PowerShell[.exe] -Help | -? | /?-PSConsoleFile    Loads the specified Windows PowerShell console file. To create a console    file, use Export-Console in Windows PowerShell.-Version    Starts the specified version of Windows PowerShell.-NoLogo    Hides the copyright banner at startup.-NoExit    Does not exit after running startup commands.-Sta    Start the shell using a single-threaded apartment.-NoProfile    Does not use the user profile.-NonInteractive    Does not present an interactive prompt to the user.-InputFormat    Describes the format of data sent to Windows PowerShell. Valid values are    "Text" (text strings) or "XML" (serialized CLIXML format).-OutputFormat    Determines how output from Windows PowerShell is formatted. Valid values    are "Text" (text strings) or "XML" (serialized CLIXML format).-WindowStyle    Sets the window style to Normal, Minimized, Maximized or Hidden.-EncodedCommand    Accepts a base-64-encoded string version of a command. Use this parameter     to submit commands to Windows PowerShell that require complex quotation     marks or curly braces.-File    Execute a script file.-ExecutionPolicy    Sets the default execution policy for the session.-Command    Executes the specified commands (and any parameters) as though they were    typed at the Windows PowerShell command prompt, and then exits, unless     NoExit is specified. The value of Command can be "-", a string. or a    script block.    If the value of Command is "-", the command text is read from standard    input.    If the value of Command is a script block, the script block must be enclosed    in braces ({}). You can specify a script block only when running PowerShell.exe    in Windows PowerShell. The results of the script block are returned to the    parent shell as deserialized XML objects, not live objects.    If the value of Command is a string, Command must be the last parameter    in the command , because any characters typed after the command are     interpreted as the command arguments.    To write a string that runs a Windows PowerShell command, use the format:   "& {<command>}"    where the quotation marks indicate a string and the invoke operator (&)    causes the command to be executed.-Help, -?, /?    Shows this message. If you are typing a PowerShell.exe command in Windows    PowerShell, prepend the command parameters with a hyphen (-), not a forward    slash (/). You can use either a hyphen or forward slash in Cmd.exe.EXAMPLES    PowerShell -PSConsoleFile SqlSnapIn.Psc1    PowerShell -version 1.0 -NoLogo -InputFormat text -OutputFormat XML    PowerShell -Command {Get-EventLog -LogName security}    PowerShell -Command "& {Get-EventLog -LogName security}"    # To use the -EncodedCommand parameter:    $command = 'dir "c:\program files" '    $bytes = [System.Text.Encoding]::Unicode.GetBytes($command)    $encodedCommand = [Convert]::ToBase64String($bytes)    powershell.exe -encodedCommand $encodedCommand

Friday, October 11, 2013 11:38 AM

There's no built-in way to launch something as an elevated process from a command prompt or batch file.  There are third-party utilities to help with that (such as elevate.exe), or you could launch a normal PowerShell.exe process first, and use Start-Process to spawn an elevated one instead, something like this:

#
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -Command Start-Process -FilePath "$PSHOME\powershell.exe" -Verb RunAs -ArgumentList "-ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -File '%cd%\Install.ps1'"
#

Note:  I haven't tested this; you might have to fiddle with the command line, quotation marks, etc.

You could also put the code right into your script that detects whether it's running as an administrator, and re-launches itself with -Verb RunAs if not.  There are examples of code to do this on the ScriptCenter repository.