We help IT Professionals succeed at work.

Running a powershell script from batch file as DIFFERENT USER (non interactively)

Krishna V
Krishna V asked
Hi Friends,
 I need your valuable suggestion on how I can achieve the following:

OS: Windows

In short: I want to understand how can I RUN a Powershell script from a Batch file by providing domain credentials (NON-INTERACTIVELY).

Here is my scenario in detail:

I have a java program, started by a Service, which creates a batch file on the fly which will have to invoke a powershell script during runtime and the powershell script has some cmdlets which need to RUN as DOMAIN USER, otherwise those cmdlets wont return values. Currently I am using a work around like.. I am running the service as the domain user and able to get the data from cmdlets, but when I run the service as Local System account I am not getting data from cmdlet.

Can you please suggest a better way to accomplish what i wanted.. getting data from cmdlet without having to run the service as domain user. Can I run the powershell script as DOMAIN USER from within the batch file. The program which creates the batch file has domain user credentials (username & password).

Please note that WinRM is disabled in the environment, otherwise I could use Invoke-Command with details and make things work without having to run the service as domain user.

Thanks a lot for your valuable suggestions.

Watch Question

Kyle AbrahamsSenior .Net Developer


You can use psExec as an option or you could use PowerShell remoting.

$user = "username"
$pass = "password" | convertto-securestring -asplaintext -force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $user, $pass

<#this is a list of computers, but you can use just your local machine #>
$list = get-content C:\list.txt 

foreach($item in $list){
$session = new-pssession $item -Credential $cred
invoke-command -Session $session {desired thing here}

Open in new window

Top Expert 2014
Non-interactively is a problem.
In my opinion, running the service as a domain user is the way this should be done.

One other possibility I see is to create a scheduled task that runs with the credentials you want.  Then just have the batch file run the scheduled task via the schtasks.exe command on demand.


Thanks Kyle and footech for your suggestions. But my question is still un-answered as I cannot use powershell remoting.
Kyle AbrahamsSenior .Net Developer

what about ps exec?
Top Expert 2014

Neither of my suggestions mentioned PS Remoting.