Copying folders to empty folders of a certain name

Hello. I am trying to look for folders with “Admin” at the end of their name. If they have no folders in them I want PowerShell to copy a group of folders and their subfolders and put them in said folder. Seems like it should be fairly straight forward but it seems to not work, I don’t get an error but when I go back to check the folders they are still empty. Can anyone tell me what I am doing wrong? Thank you for the help

$EmptyFolders = Get-ChildItem -include *Admin, -recurse -directory 

ForEach ($item in $EmptyFolders) 

{
if ($_.GetDirectories().count -eq 0) 
{
Copy-Item -path "Z:\TECH\(1) PROJECTS & PORTFOLIOS\UPDATED Example\4. Admin","Z:\TECH\(1) PROJECTS & PORTFOLIOS\UPDATED Example\ItemsToCheck",
"Z:\TECH\(1) PROJECTS & PORTFOLIOS\UPDATED Example\1. Customers","Z:\TECH\(1) PROJECTS & PORTFOLIOS\UPDATED Example\2. Outputs",
"Z:\TECH\(1) PROJECTS & PORTFOLIOS\UPDATED Example\3. Teams" -Recurse -Destination $_.FullName -Confirm}}

$_.FullName should be $item.FullName

Thank you for the assist. Still looks like it’s doing the same thing, not moving the folders. I apologize, there are details that may have been helpful to put in. 1. Is I don’t have admin privileges, the Set-ExecutionPolicy -Scope -CurrentUser even gives me an error. 2. The files are on a shared drive. These may not be relevant but thought I should put them in anyways.

A couple of things to try. You should not need to change Powershell execution privileges unless you save the file as a PS1, but you can test in ISE. I would start by just highlighting and executing the first 2 lines. The emptyFolders doesn’t have a path, so you have to be in the current directory to return the folders, but if you are not, then it would be NULL:

PS C:\Users\rasim> $emptyFolders = Get-ChildItem -include *Admin, -Recurse -Directory

PS C:\Users\rasim> $emptyFolders

PS C:\Users\rasim> 

If it’s NULL, you are not processing anything. It’s also better if you get the items you want to copy to ensure you are copying what you expect. Then you should add some debug lines to make sure the script is processing as expected and nothing is null, like your directory count because it will never run that block.

$emptyFolders = Get-ChildItem -include *Admin, -Recurse -Directory
$templateFolders = Get-ChildItem "Z:\TECH\(1) PROJECTS & PORTFOLIOS\UPDATED Example\*"

foreach ($folder in $EmptyFolders) {

    'Processing directory {0} with {1} directories' -f $folder.FullName, $folder.GetDirectories().count

    if ($folder.GetDirectories().count -eq 0) {
        'Found empty directory {0}, beginning copy operations' -f $folder.FullName
        $templateFolders | Copy-Item -Recurse -Destination $folder.FullName -Confirm
    }

}

This one is interesting. I can come up at least one thing to solve in my daily-ish work.

[pre]
$folders = Get-ChildItem -Recurse -Directory c:\temp | where {$.name -like “*admin”} | select fullname,@{N=‘SubDirectories’;E={[boolean]($ | get-childitem -directory).count}}
$folders
$folders | where {-not $.subdirectories} | foreach {Copy-Item C:\temp\fwnet\ $.fullname -verbose}
[/pre]

Of course you can make it even a one liner, but I prefer to keep it more readable and split the stuff to multiple lines.

Thank you. It worked. One reason I may have not been getting all files was because the path names were too long. When I ran your code I got:

Copy-Item : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 
characters, and the directory name must be less than 248 characters.

Your code works though from what I see I will just have to minimize our folder structure in the future, thank you again.

Thank you sir, this code works as intended as well. Had 2 question for you just for my knowledge:

  1. These two lines"
 'Processing directory {0} with {1} directories' -f $folder.FullName, $folder.GetDirectories().count

and

'Found empty directory {0}, beginning copy operations' -f $folder.FullName

Still trying to understand how code is formatted. Are these just informative comments?

  1. Also the 260 character limit for path names in windows… I assume there’s no simple way around that with PowerShell?
    Thank you again for the help.