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 would like to write a powershell script that gets the following parameters as input:
Folder to copy from, extensions allows, folder to copy to and a boolean indicating if the change should restart IIS, username and password.
What cmdlets should I be looking at considering that I am copying to a remote server?
How do I read the parameters into variables?
How do I restart IIS?

Cosidering that I might want to copy multiple folders, how do I write a powershell script that invokes a powershell script?

share|improve this question
Wow, sorry for a delay in any response... Do you still need help? – Marco Shaw Dec 18 '10 at 21:49
Yes, I don't really know how to restart IIS? How do I make sure the files are really copied? – the_drow Dec 18 '10 at 23:20
up vote 15 down vote accepted

Get-ChildItem allows you to list files and directories, including recursively with filename filters. Copy-Item allows you to copy a file.

There is a lot of overlap in terms of selecting the files, often Copy-Item on its own is sufficient depending on the details of what you need (eg. do you want to retain the folder structure?)

To copy all *.foo and *.bar from StartFolder to DestFolder:

Copy-Item -path "StartFolder" -include "*.foo","*.bar" -Destination "DestFolder"

If you need to preserve the folder structure things get harder because you need to build the destination folder name, something like:

$sourcePath = 'C:\StartFolder'
$destPath = 'C:\DestFolder'

Get-ChildItem $sourcePath -Recurse -Include '*.foo', '*.bar' | Foreach-Object `
        $destDir = Split-Path ($_.FullName -Replace [regex]::Escape($sourcePath), $destPath)
        if (!(Test-Path $destDir))
            New-Item -ItemType directory $destDir | Out-Null
        Copy-Item $_ -Destination $destDir

But robocopy is likely to be easier:

robocopy StartFolder DestFolder *.foo *.bar /s

In the end the way to choose will depend on the details of what's needed.

(In the above I've avoided aliases (e.g. Copy-Item rather than copy) and explicitly use parameter names even if they are positional.)

share|improve this answer

I can't address the IIS portion, but the file copy while preserving the directory structure can be a lot simpler than shown in the other answers:

Copy-Item -path "StartFolder" -Recurse -Include "*.foo","*.bar" -Destination "DestFolder" -Container

The -Container argument is the magic part that will replicate the structure in the destination as it is in the source.

share|improve this answer

If you are using IIS7, you can use native component to replicate content between IIS servers:

share|improve this answer

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.