Link to home
Start Free TrialLog in
Avatar of Robin Lemon
Robin LemonFlag for Australia

asked on

Error Creating a folder under Program Files in command prompt or script

When I am looged into a Windows 7 PC as a Domain Administrator, I can create a folder under C:\Program Files using the Windows Explorer interface.

However, when I try and create a folder in a Command prompt under C:\Program Files, I get "Access Denied". I also get an access denied error when creating a folder under C:\Program Files using a Powershell or VB Script.

How do I overcome this problem? I specifically want to be able to create a directory using Powershell

e.g.    New-Item "C:\Program Files\BGInfo" -type directory
Avatar of kortina
kortina

You might find that UAC (User Account Control) is getting you.

Try run a command prompt as administrator. Not just running cmd, you need to click Start, Accessories, Command Prompt. Right Click and choose "Run as Administrator"

When the command prompt runs the title should say "Administrator:Command Prompt"

Then try run your powershell script or use mkdir to create your folder.

Likewise, your powershell environment might not be starting as admin.
Avatar of Robin Lemon

ASKER

Hi Kortina
You are right. If I log into Windows 7 as a standard user and right click on the Command Prompt and select "Run as Administrator" and put in the domain administrator credentials, I can create the folder.
However, that still leaves me with the problem of how to start my scripting environment programatically as Admin.
 
runas /user:"Domain\administrator"  cmd
will open a cmd prompt or substitute cmd for the program or call your batch scipt...
runas /? will give you the full options
The problem is it will still prompt for a password
You could try this free program that behaves similaly to runas with an encrypted password
http://www.commandline.co.uk/sanur/ 
Good luck
Avatar of McKnife
I think beanrod meant to link runasspc as sanur does not even use encryption. But both don't elevate.
--
It is a bad practice to write into program files - avoid it if possible.
Are you trying to create a script that your clients will use?
This will only be relevant if you want BGINFO in a Domain envronment works on all versions of Windows I've tested...
If you want you to use Group Policy you could create a "log on" script. At my workplace I have implemented a VB startup script that loads bginfo from a serverlocation rather than a local location.
I have customised settings and need not deploy the physical install. In a multi Domain Controller envirnoment simply put it in the SYSVOL share and it will pick the DC based on a least cost route. So if it is for BGINFO if "Authenticated Users" have Read & Execute permissions you get a nice BGINFO background. Very handy when you can't remember peoples names. :)

Create a document and rename it to "Scirptname.vbs"
Copy and paste below code and change the directories as you see fit...
The CCsettings.bgi is a saved file of custom config settings for BGinfo.

Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "\\Servername\Netlogon\BGinfo\BGinfo.exe \\ServerName\Netlogon\BGinfo\CCSettings.bgi /accepteula /timer:0", 1, True
wrong thread?
To MCKnife, Umm well he did mention BGinfo so I thought perhaps he needed the elevated priveledges to have local copies of BGinfo. You are right will probably no good APAGroup but thought I would share just on the off chance that it may help. Thanks for pointing out that SANUR wouldn't work must be getting tired, I typically use "Start-Up" scripts not to be confused with "logon scripts" to get elevated rights as they run in the system context but that is going off on another tangent.
You could try the suggestion here:
http://stackoverflow.com/questions/1566969/showing-the-uac-prompt-in-powershell-if-the-action-requires-elevation

I havent used it myself.

Depending on how you plan to run your powershell script you could always right click on the script and choose run as admin.
ASKER CERTIFIED SOLUTION
Avatar of Robin Lemon
Robin Lemon
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I am away for a Christmas break but will be back onto this issue in the New Year.