Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have created a PowerShell script for copying files to a directory, the script, first creates a folder , or forces a new folder event if it exists. Then copies a directory from another location. After copying, the files I then need to copy the correct web config depending on a value given by the user execturing the script. The issue I am having is I can copy the files, but all the files are set to read-only meaning when I try and copy the correct web.config, the script fails as access is denied.

This is a cut down version of script for simplicity.

$WebApp_Root = 'C:\Documents and Settings\user\Desktop\Dummy.Website'

$Preview_WebApp_Root = 'c:\applications\Preview\'

$Choice = read-host("enter 'preview' to deploy to preview, enter Dummy to deploy to Dummy, or enter test to deploy to the test environment")
if (($Choice -eq 'Preview') -or ($Choice -eq 'preview'))
$Choice = 'Preview'
$Final_WebApp_Root  = $Preview_WebApp_Root

write-host("Releasing Build to " + $Choice +'...')

write-host("Emptying web folders or creating them if they don't exist... ")
New-Item $Final_WebApp_Root -type directory -force 

write-host("Copying Files... ")
Copy-Item $WebApp_Root $Final_WebApp_Root -recurse  

write-host("Copy the correct config file over the top of the dev web config...")
Copy-Item $Final_WebApp_Root\Config\$Choice\Web.configX $Final_WebApp_Root\web.config

write-host("Copying correct nhibernate config over")
Copy-Item $Final_WebApp_Root\Config\$Choice\NHibernate.config $Final_WebApp_Root\NHibernate.config

write-host("Deployed full application to environment")
share|improve this question
up vote 0 down vote accepted

When you do your Copy-Item, use the -Force parameter to get around the read-only attribute thing. If that doesn't get you by it, then you can add a routine to use Get-Item and temporarily remove the Read-Only attribute, copy the file, then reset the attribute.

Hope this helps.

share|improve this answer
Thanks this worked great – user48890 Jul 22 '10 at 8:39

Your Answer


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.