0

I have problem with path. I need to copy all files from directory I add as 1st parameter in Powershell command.

Now I have:

Copy-Item -Path "$args[0]/" -Filter*.*

So it will copy to location I am now (and it's ok, I don't want other location) but it doesn't copy anything. Please help.

5
  • 3
    please add more code, for example the function this is called in Commented Oct 21, 2014 at 20:17
  • 2
    Based strictly on the code that you have listed you can not copy things because copying an item requires both SOURCE and DESTINATION paths to be supplied in some form. You provide one of the two, and need to supply the other. Per the syntax you use the path you have supplied is the source. You would need to add the -destination argument to specify where the files should be copied to. Commented Oct 21, 2014 at 20:37
  • But what if I want to copy it to location I am now? I change the set-location and I still need to add Destination path? Commented Oct 21, 2014 at 20:43
  • According to TechNet -Destination is optional. What are you passing as $args[0] for example. Please show a code sample Commented Oct 21, 2014 at 20:49
  • C:\Users\Administrator\Desktop\test.ps1 C:\TEST And in directory C:\TEST I've got some files which I want to copy to my current location. But with my current code, it copies folder TEST (without files in) and I want only files from TEST. Commented Oct 21, 2014 at 20:59

2 Answers 2

2

Pretty sure the issue is that the 0 element in $args is not expanding in the string creating an invalid path. Wrap the variable in $() to allow it to expand inside a double quoted string. Else you would end up trying to copy the folder C:\TEST[0]/ which obviously is not correct.

Copy-Item -Path "$($args[0])/" -Filter *.* -Recurse

Not yet sure why you have a forward slash in there since Windows pathing uses backslashes.

function Copy-Something(){
    test-Path "$($args[0])/"
    test-path "$($args[0])"
}

Copy-Something C:\temp

With what little you have provided the output shows that it might be redundant to have the slash there. Would also recommend calling Test-Path on the argument anyway as it might have caught this for you.

True
True

From Question Comment

You are looking for the -Recurse parameter if you also want folder contents.

From Answer Comment

If you want the contents and not the folder you should be able to do something like this:

Copy-Item -Path "$($args[0])\*" -Filter *.* -Recurse
Sign up to request clarification or add additional context in comments.

2 Comments

So now I use: C:\Users\Administrator\Desktop\test.ps1 C:\TEST and i code I have: Copy-Item -Path "$($args[0])" -Filter*.* -Recurse BUT it copies directory TEST with files in it. I want only files (without directory TEST). How to do it ? Thanks for help.
@Roberto look at the bottom of my answer
0

When in doubt, Get-Help Command.

PS C:\> Get-Help Copy-Item -ShowWindow

-------------------------- EXAMPLE 3 --------------------------
This command copies the contents of the C:\Logfiles directory
to the C:\Drawings\Logs directory. It creates the \Logs
subdirectory if it does not already exist.

Windows PowerShell
PS C:\> Copy-Item C:\Logfiles -Destination C:\Drawings\Logs -Recurse

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.