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 a bat file which will delete files in some folder (Profile Folder). But i do-not have access to that folder. But another account has access to the folder. How to run a batch file from one account by login into the another account which has access and delete those files?

share|improve this question

2 Answers 2

Use the runas command.

or

In Windows Explorer you can right-click on the bat file and choose Run As... then enter your credentials.

or

You could run it from a scheduled task which lets you set credentials.

share|improve this answer
1  
Props for the Scheduled Task idea - that's lateral thinking! –  Izzy Sep 23 '09 at 18:14

You have two options. One means you'll have to manually pass the password each time (not ideal), the other will let you pass with password from the batch file (but this means anyone can read the batch file and discover the password). You choose which flaw you would rather live with:


Option 1 (Manual Password Entry)

rem This is the test.bat file
runas /noprofile /env /user:myDomain\myAccount "cmd.exe /c del c:\temp\*.log"


Option 2 (Automatic Password Entry)

Use the PsExec command from Microsoft SysInternals PsTools suite:

rem This is the test.bat file
psexec -u myDomain\myAccount -p myPassword -d -e cmd.exe /c "del c:\temp\*.log"


Please note: The quote marks are in different places for each example. This is intentional. Both commands have have quirks when it comes to passing a command with lots of arguments/switches. The quote marks need to be in the exact locations I've displayed (in relation to command <arguments>)

share|improve this answer
    
+1 Or do this :) –  squillman Sep 23 '09 at 18:09

Your Answer

 
discard

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