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?

  • Wow, sorry for a delay in any response... Do you still need help?
    – Marco Shaw
    Dec 18, 2010 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, 2010 at 23:20

5 Answers 5


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.)

  • 2
    Doesnt work, I tried: Copy-Item -path "C:\Users\username\Pictures\" -include "*.JPG", "*.PNG" -Destination "D:\" with and without -recurse - nothing happens Oct 28, 2017 at 22:41

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.

  • 1
    I'm unable to get this to work with -Include... though I can get it to copy everything without -Include Sep 26, 2017 at 14:22

The only solution that worked for me is

Copy-Item -path 'c:\SourceFolder\*.foo' -destination 'c:\DestFolder'

Other solutions that use -Include parameter didn't work.


As some people mentioned here -Include doesn't work.

To make it work with nested folders I used two steps approach: 'filter' + delete empty folders

#copy with folder structure
Copy-Item -path $source -Recurse -Filter "*.foo" -Destination $destination -Container

#remove empty folders
dir $destination -Directory -Recurse |
    %{ $_.FullName} |
    sort -Descending |
    where { !@(ls -force $_) } |

If you are using IIS7, you can use native component to replicate content between IIS servers: http://blog.theplanet.com/2010/05/18/mirroring-server-content-and-configuration-in-iis7/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.