Search code examples
powershellpathcopy-item

path as parameter powershell


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.


Solution

  • 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