I am trying to copy a file from the newest folder in on directory to another folder. I have made the code below but it seems that the copy-item command does not work with variables. Does anyone have any other ideas?

# Code to get newest directory

$newestFolder = gci \\Server123\sharepoint\HCL\HCL-SUPPORT-SHARED\REPORTING\CAPACITY-PLANNING\ | ? { $_.PSIsContainer } | sort CreationTime -desc | select -f 1

<# get the latest created folder from a path #>

# Copy folder to a folder with different name

Copy-Item $newestFolder.FullName -Destination "\\Server123\sharepoint\HCL\HCL-SUPPORT-SHARED\REPORTING\CAPACITY-PLANNING\SP Capacity Planning" -Recurse

Error Message:

Copy-Item : Cannot find path '\\Server123\sharepoint\HCL\HCL-SUPPORT-SHARED\REPORTING\CAPACITY-PLANNING\SP Capacity Planning\Average-Action.csv' because it does not exist. At line:26 char:1 + Copy-Item -Path $sourcedirectory\Average-Action.csv -Destination $tar ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (\\Server123...rage-Action.csv:String) [Copy-Item], ItemNo tFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand 

Copy-Item : Cannot find path '\Server123\sharepoint\HCL\HCL-SUPPORT-SHARED\REPORTING\CAPACITY-PLANNING\S P Capacity Planning\MaxDuration20.csv' because it does not exist. At line:47 char:1 + Copy-Item -Path $sourcedirectory1\MaxDuration20.csv -Destination $tar ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  • what makes you think it doesn't work with variables? what doesn't work? Do you get an error message - if so please include it. Else please explain what goes wrong. You'll never find a file in your gci because you filter only Directories with $_.PSIsContainer
    – SimonS
    Dec 9, 2019 at 13:21
  • So if i do not user "gci" then what do i use?
    – joe 222
    Dec 9, 2019 at 17:08
  • The code you shared worked fine for me. You'll need to share more details of the script to troubleshoot further. Based on the errors, it seems like you're having an issue with a specific file. Any chance another job is deleting a file between the time gci determines the newest folder and copy-item gets to the file in question to attempt the copy?
    – lightwing
    Dec 9, 2019 at 20:28

1 Answer 1


Double-check that you are actually copying a directory to a directory, because the error you posted is using a file name instead of a folder.

Try the following using your paths:

$sourcefolder = '\\path\to\folder\'
$targetfolder = '\\path\to\target\'
$newestfolder = gci -Directory $sourcefolder | sort CreationTime -desc | select -f 1  ## get the newest folder
copy -recurse $newestfolder $targetfolder
  • I TRIED YOU APPROACH BUT tHE COMMAD FOR COPYING IS NOT WORKING. $sourcefolder = '\\path\to\folder\' $targetfolder = '\\path\to\target\' $newestfolder = gci -Directory $sourcefolder | sort CreationTime -desc | select -f 1 ## get the newest folder Copy-Item -Path $newestfolder -Destination $targetfolder -Recurse
    – joe 222
    Feb 23, 2020 at 11:46
  • ERROR: copy-Item : Cannot find path '.......PathInQuestion......' because it does not exist. At line:6 char:1 + Copy-Item -Path $newestfolder -Destination " \\path\to\target\ + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (.......PathInQuestion......) [Copy-Item], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand Is this error an access issue?
    – joe 222
    Feb 23, 2020 at 11:46
  • try and do get-item for the path and see if powershell gives a more specific error. If the path is above the max character length, you may get this error.
    – Cpt.Whale
    Feb 23, 2020 at 17:08
  • the path is less then "Powershell Overcoming 260 character path limit. 260 characters, and the directory name must be less than 248 characters." Thus... can get-item except a variable as a path?
    – joe 222
    Feb 24, 2020 at 11:50
  • yes, get-item $newestfolder should work fine. You only need to look for the error causing your copy-item to fail. It may be permissions, or a few other reasons. It will be helpful to know if you can read and copy a "bad" item by itself.
    – Cpt.Whale
    Feb 25, 2020 at 14:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .