2

A little heads up that this is my first powershell scripting project. I'm trying to write a script that prompts the user for a source file and a directory to copy it into. The script runs with no errors but doesn't actually copy the file. Any help would be greatly appreciated. A usage example would be: copy C:\Users\User\Desktop\info.txt to C:\Users\User\Documents\*

$source = Get-ChildItem -Path (Read-Host -Prompt 'Enter the full name of the file you want to copy')
$dirs = Get-ChildItem -Path (Read-Host -Prompt 'Enter the full name of the directory you want to copy to')  

foreach ($dir in $dirs){
   copy $source $dir
}

1 Answer 1

3

I made a few changes to your script that make it work, you may need to tweak it a little bit to accomplish your goals:

$source = Get-Item -Path (Read-Host -Prompt 'Enter the full name of the file you want to copy')
$dirs = Get-ChildItem -Path (Read-Host -Prompt 'Enter the full name of the directory you want to copy to')  
foreach ($dir in $dirs){
Copy-Item $source $dir.FullName
}

First, I changed $source from Get-ChildItem to Get-Item because you specified that it should find a single file.

From there, when I ran the script, I noticed that instead of files being made in the directories, it just made a bunch of files that were named identically to the directories.

To investigate this behavior, I added -whatif to the end of the Copy-Item commandlet.

Copy-Item $source $dir -whatif

This gave me the following output:

What if: Performing operation "Copy File" on Target "Item: H:\test\source\test.txt Destination: H:\test\Folder1".

What if: Performing operation "Copy File" on Target "Item: H:\test\source\test.txt Destination: H:\test\Folder2".

What if: Performing operation "Copy File" on Target "Item: H:\test\source\test.txt Destination: H:\test\Folder3".

What if: Performing operation "Copy File" on Target "Item: H:\test\source\test.txt Destination: H:\test\Folder4".

This explained the weird output of the script, it was misunderstanding the destination. Sometimes Powershell doesn't understand what you're trying to do so you have to be more explicit.

I then ran the following command:

$dir | select *

This gives a lot of properties, but the important one is this:

FullName : H:\test\Destination\Folder4

So I changed up the script to this:

Copy-Item $source $dir.FullName

After making these changes, running the script copied the test.txt file I specified into each of the subdirectories in the destination folder.

Sign up to request clarification or add additional context in comments.

2 Comments

Awesome! Thanks for the explanations. The only thing that I noticed is that when copying to a destination that includes shortcuts, those shortcuts are ruined. That can fixed with an exception for those files though, correct?
To copy to directories recursively and not copy into non-directories use $dirs = Get-ChildItem -Path . -Directory -Recurse

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.