Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question
    
Do you want the PowerShell script or the cmd.exe prompt to be hidden? –  Mark Schill Jun 27 '10 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. –  Јοеу Jun 27 '10 at 21:35
    
@Mark - I want the command window to be hidden –  Ben Jun 27 '10 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 '10 at 22:24
    
Taken a look at Start-Process? –  Јοеу Jun 28 '10 at 9:32
add comment

3 Answers

up vote 0 down vote accepted

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()
share|improve this answer
add comment

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

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

share|improve this answer
    
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 '10 at 10:44
add comment

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}'"
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.