3

I have written a powershell script which generates a "EULA" type popup which the user has to agree to.

It does this at logon by running as a scheduled task for a user (non-admin) account. It needs to run elevated, so I am using the following script to run it elevated:

$pw= convertto-securestring "myPassw0rd" -asplaintext –force
$credential = new-object -typename system.management.automation.pscredential -argumentlist "-default-",$pw
$localArgs = "/c Powershell c:\scripts\myScript.ps1" 

[System.Diagnostics.Process]::Start("cmd.exe", $localArgs, "Administrator", $credential.Password, $computer)

(I will be encrypting the password to make it slightly more secure, but that's not relevant to this question.)

Anyway - my problem is that when the script is called it displays the command prompt window behind my "pretty" EULA popup.

Is there a way to hide / minimise the command window?

Thanks,

Ben

5
  • Do you want the PowerShell script or the cmd.exe prompt to be hidden? Jun 27, 2010 at 21:24
  • Why do you even run a PowerShell script by calling cmd which then starts PowerShell? That sounds like at least two levels of indirection too much.
    – Joey
    Jun 27, 2010 at 21:35
  • @Mark - I want the command window to be hidden
    – Ben
    Jun 27, 2010 at 22:22
  • @Johannes - That was the only way I could successfully pass Admin credentials to it, by running the above "wrapper" script
    – Ben
    Jun 27, 2010 at 22:24
  • Taken a look at Start-Process?
    – Joey
    Jun 28, 2010 at 9:32

3 Answers 3

2

This should be what you need:

$Process = new-Object System.Diagnostics.Process
$Process.StartInfo.UserName="Administrator"
$Process.StartInfo.Password=$Credential.Password
$Process.StartInfo.Domain="$Computer"
$Process.StartInfo.WindowStyle="Hidden"
$Process.StartInfo.FileName="cmd.exe"
$Process.StartInfo.Arguments="$localArgs"
$Process.Start()
1

You can use the Start-Process cmdlet (PowerShell 2.0):

Start-Process cmd.exe -Credential $credential -WindowStyle Hidden -WorkingDirectory ... -ArgumentList...

1
  • Think this is 90% there, but am getting " Parameter set cannot be resolved using the specified named parameters" when I add the -Credential argument.
    – Ben
    Jun 29, 2010 at 10:44
1

You can't use -Credential and -WindowStyle parameters together with PowerShell v2, you either need PowerShell v3 or use -NoNewWindow and -Credential parameters together

You can use the below code for PowerShell v2:

$user = "{user}"
$pass = ConvertTo-SecureString -String "{password}" -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential `
     -argumentlist $user, $pass

start-process -Credential $cred -NoNewWindow powershell "-command & '{path and script}'"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .