1

I have the following script

$sourceRoot = "C:\Users\skywalker\Desktop\deathStar\server"
$destinationRoot = "C:\Users\skywalker\Desktop\deathStar/server-sandbox"
$dir = get-childitem $sourceRoot  -Exclude .env, web.config   

Write-Output "Copying Folders"
$i=1
$dir| %{
    [int]$percent = $i / $dir.count * 100
    Write-Progress -Activity "Copying ... ($percent %)" -status $_  -PercentComplete $percent -verbose
    copy -Destination $destinationRoot  -Recurse -Force
    $i++

I tried to reference this post, but I ended up getting the following prompt in the powershell console.

Supply values for the following parameters:

Path[0]:

5
  • what is the rest of the error text? it _usually includes the line number AND at least part of the line of code ... Commented Sep 11, 2019 at 4:26
  • It’s not an error it’s actually asking me to input values for the path[0], because if I type something then it goes to path[1]... so I’m not sure what’s going on. And I should add I’m a newbie PS scripted Commented Sep 11, 2019 at 4:33
  • 1
    ah! i misunderstood. [blush] so ... do you know what cmdlet is producing that prompt? i suspect it is the copy -Destination line since you have that INSIDE a ForEach-Object block ... and didn't pass any source to it. Commented Sep 11, 2019 at 5:07
  • No worries, you were right for sure. Commented Sep 11, 2019 at 13:37
  • kool! glad that you got it working ... [grin] Commented Sep 11, 2019 at 13:47

1 Answer 1

2

You're using % (ForEach-Object) to process input from the pipeline ($dir) object by object.

Inside the script block ({ ... }) that operates on the input, you must use the automatic $_ variable to reference the pipeline input object at hand - commands you use inside the script block do not themselves automatically receive that object as their input.

Therefore, your copy (Copy-Item) command:

copy -Destination $destinationRoot  -Recurse -Force

lacks a source argument and must be changed to something like:

$_ | copy -Destination $destinationRoot  -Recurse -Force

Without a source argument (passed to -Path or -LiteralPath) - which is mandatory - Copy-Item prompts for it, which is what you experienced (the default parameter is -Path).

In the fixed command above, passing $_ via the pipeline implicitly binds to Copy-Item's -LiteralPath parameter.

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.