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 am trying to copy a file named 'dat'. This file has no extension. The thing is though that under a Parent Folder 'Personal' I have 500 folders and all of these 500 folders have the 'dat' file. I am interested in copying all these 'dat' files and placing them in my destination directory.

So when I paste these 'dat' files in my destination directory, I am hoping to name them as follows:


So all the dat files will be renamed to the folder_name they were residing under with the extension'dat'.

Is this possible?

share|improve this question
Are the 500 folders all in the root of "personal" or are can they be nested within eachother? – jscott Jan 8 '13 at 17:59
They are all under "Personal". The directory structure looks like below: PS L:\usr\PersonalFolders> tree Folder PATH listing for volume J: L:. ├───133MHz00000001 ├───166MHz ├───2.5V drivers ├───2.5V Standards ├───2006 SanFrancisco ├───200MHz frontside bus – David Jan 8 '13 at 18:07
up vote 4 down vote accepted

Configure $source and $dest as needed, include trailing slash.

$source = "H:\personal\"
$dest   = "H:\archive\"

Get-ChildItem -Recurse -Path $source |
    Where-Object { $_.PsIsContainer -eq $false -and $_.Name -eq "dat" } |
        ForEach-Object { $_.CopyTo( $( $dest + $_.Directory.Name + ".dat" ), $true ) }
share|improve this answer
It worked like a charm. Thx. – David Jan 8 '13 at 18:44
PS H:\> foreach($_ in Get-ChildItem dat -Recurse) { Copy-Item $_.Fullname H:\dest\$($_.Directory.ToString().Split("\")[-1])`.dat }
share|improve this answer
Neat, I've used ` for lots of other things, but never to escape the syntax. +1. – jscott Jan 8 '13 at 18:13
Thanks. +1 for you too because let's have a happy Powershell party. :) – Ryan Ries Jan 8 '13 at 18:15

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.