2

I am an administrator and learning about scripts but was trying to create a script to open multiple programs as another user (my admin password)

The following script runs well when run from powershell ise but won't run properly when right clicking and selecting "run with powershell"

Start-Process -FilePath "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" -Credential "domain\userid"

When run it prompts for password but doesn't bring up google chrome. It works OK when run from Powershell ISE and I get error "The directory name is invalid" when running the script from Powershell.exe (not the ISE environment).

ATek
  • 815
  • 2
  • 8
  • 20
Brian862107
  • 21
  • 1
  • 2
  • unable to reproduce the issue on my windows server 2012 with powershell 4.0. The command works the same in my powershell console & powershell ISE – Kai Zhao Apr 22 '16 at 16:43
  • If you run `start-process -filepath c:\asdfasdf\asdf.exe` the error which comes back is *"This command cannot be run due to the error: The system cannot find the file specified"*, not *"The directory name is invalid"*. I guess this is either in the configuration of your context menu "Run with PowerShell", maybe it has an invalid path to the PowerShell interpreter, or the PowerShell.exe process can't read the path to your script (but I can't reproduce that error here with some quick testing). Is the error a PowerShell exception or an Explorer dialog box? – TessellatingHeckler Apr 22 '16 at 18:04
  • What if you were to execute it in this fashion from the RUN menu or even a cmd prompt? Same directory name is invalid message? `powershell.exe -nop "C:\path to your script.ps1"` – ATek Apr 22 '16 at 18:56
  • ssaviers - that worked but ideally I would to be able to double click a batch file or just right click on the powershell file and run the file. Tessheck - it's that full error message you suggested, tried to screenshot but don't think you can on this forum, it shows this on powershell when I tried to run it from there:- I did this to troubleshoot it further but don't understand why I'm getting this error when directory is correct and tried running with policy unrestricted and remote signed. – Brian862107 Apr 24 '16 at 11:51
  • Seems to have something to do with my admin rights, cmd as admin worked ok but issue replicated when i run it in command prompt without admin – Brian862107 Apr 24 '16 at 11:58
  • See the selected answer in this post: http://stackoverflow.com/questions/7319658/start-process-raises-an-error-when-providing-credentials-possible-bug/24076970#24076970 – d.breve Dec 22 '16 at 17:52

1 Answers1

4

Add the working directory:

Start-Process `
    -FilePath "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" `
    -Credential "domain\userid" `
    -WorkingDirectory "C:\Program Files (x86)\Google\Chrome\Application\"
Maximilian Burszley
  • 18,243
  • 4
  • 34
  • 63
d.breve
  • 127
  • 1
  • 12